Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/controller/story_controller.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:rxdart/rxdart.dart';
import 'package:flutter/foundation.dart';

enum PlaybackState { pause, play, next, previous }
enum PlaybackState { pause, play, next, previous, remove }

/// Controller to sync playback between animated child (story) views. This
/// helps make sure when stories are paused, the animation (gifs/slides) are
Expand Down Expand Up @@ -29,6 +30,11 @@ class StoryController {
playbackNotifier.add(PlaybackState.previous);
}

/// Call this to remove the current story from the StoryView.
void removeCurrentStory() {
playbackNotifier.add(PlaybackState.remove);
}

/// Remember to call dispose when the story screen is disposed to close
/// the notifier stream.
void dispose() {
Expand Down
79 changes: 62 additions & 17 deletions lib/widgets/story_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ class StoryItem {
bottom: Radius.circular(roundedBottom ? 8 : 0),
),
),
padding: textOuterPadding?? EdgeInsets.symmetric(
horizontal: 24,
vertical: 16,
),
padding: textOuterPadding ??
EdgeInsets.symmetric(
horizontal: 24,
vertical: 16,
),
child: Center(
child: Text(
title,
Expand Down Expand Up @@ -140,12 +141,13 @@ class StoryItem {
margin: EdgeInsets.only(
bottom: 24,
),
padding: captionOuterPadding?? EdgeInsets.symmetric(
horizontal: 24,
vertical: 8,
),
padding: captionOuterPadding ??
EdgeInsets.symmetric(
horizontal: 24,
vertical: 8,
),
color: caption != null ? Colors.black54 : Colors.transparent,
child: caption?? const SizedBox.shrink(),
child: caption ?? const SizedBox.shrink(),
),
),
)
Expand Down Expand Up @@ -193,11 +195,12 @@ class StoryItem {
),
Container(
margin: EdgeInsets.only(bottom: 16),
padding: captionOuterPadding?? EdgeInsets.symmetric(horizontal: 24, vertical: 8),
padding: captionOuterPadding ??
EdgeInsets.symmetric(horizontal: 24, vertical: 8),
child: Align(
alignment: Alignment.bottomLeft,
child: Container(
child: caption?? const SizedBox.shrink(),
child: caption ?? const SizedBox.shrink(),
width: double.infinity,
),
),
Expand Down Expand Up @@ -252,7 +255,7 @@ class StoryItem {
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 8),
color:
caption != null ? Colors.black54 : Colors.transparent,
child: caption?? const SizedBox.shrink(),
child: caption ?? const SizedBox.shrink(),
),
),
)
Expand Down Expand Up @@ -406,6 +409,7 @@ class StoryView extends StatefulWidget {

/// Indicator Color
final Color? indicatorColor;

/// Indicator Foreground Color
final Color? indicatorForegroundColor;

Expand All @@ -427,7 +431,10 @@ class StoryView extends StatefulWidget {
this.indicatorColor,
this.indicatorForegroundColor,
this.indicatorHeight = IndicatorHeight.large,
this.indicatorOuterPadding = const EdgeInsets.symmetric(horizontal: 16, vertical: 8,),
this.indicatorOuterPadding = const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
});

@override
Expand All @@ -445,6 +452,37 @@ class StoryViewState extends State<StoryView> with TickerProviderStateMixin {

VerticalDragInfo? verticalDragInfo;

/// Removes the current story from the list and resets playback to the next story.
void removeCurrentStory() {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New function here. All above in this file is just formatting changes.

final currentStory = _currentStory;
if (currentStory == null) return;
// If only one story remains, just complete playback and do not remove
if (widget.storyItems.length == 1) {
_onComplete();
return;
}
final currentIndex = widget.storyItems.indexOf(currentStory);
final wasLastStory = (currentIndex == widget.storyItems.length - 1);
setState(() {
widget.storyItems.removeAt(currentIndex);
});
// If there are no stories left, just stop playback
if (widget.storyItems.isEmpty) {
_animationController?.stop();
return;
}
// If we just removed the last story, call onComplete
if (wasLastStory) {
_onComplete();
return;
}
// Reset all stories after the removed index to not shown
for (int i = currentIndex; i < widget.storyItems.length; i++) {
widget.storyItems[i]!.shown = false;
}
_beginPlay();
}

StoryItem? get _currentStory {
return widget.storyItems.firstWhereOrNull((it) => !it!.shown);
}
Expand Down Expand Up @@ -495,6 +533,10 @@ class StoryViewState extends State<StoryView> with TickerProviderStateMixin {
_removeNextHold();
_goBack();
break;
// Support removal via playbackNotifier
case PlaybackState.remove:
removeCurrentStory();
break;
}
});

Expand Down Expand Up @@ -796,8 +838,11 @@ class PageBarState extends State<PageBar> {
right: widget.pages.last == it ? 0 : this.spacing),
child: StoryProgressIndicator(
isPlaying(it) ? widget.animation!.value : (it.shown ? 1 : 0),
indicatorHeight:
widget.indicatorHeight == IndicatorHeight.large ? 5 : widget.indicatorHeight == IndicatorHeight.medium ? 3 : 2,
indicatorHeight: widget.indicatorHeight == IndicatorHeight.large
? 5
: widget.indicatorHeight == IndicatorHeight.medium
? 3
: 2,
indicatorColor: widget.indicatorColor,
indicatorForegroundColor: widget.indicatorForegroundColor,
),
Expand Down Expand Up @@ -831,11 +876,11 @@ class StoryProgressIndicator extends StatelessWidget {
this.indicatorHeight,
),
foregroundPainter: IndicatorOval(
this.indicatorForegroundColor?? Colors.white.withValues(alpha: 0.8),
this.indicatorForegroundColor ?? Colors.white.withValues(alpha: 0.8),
this.value,
),
painter: IndicatorOval(
this.indicatorColor?? Colors.white.withValues(alpha: 0.4),
this.indicatorColor ?? Colors.white.withValues(alpha: 0.4),
1.0,
),
);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ packages:
dependency: transitive
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
url: "https://pub.dev"
source: hosted
version: "2.1.4"
version: "2.2.0"
video_player:
dependency: "direct main"
description:
Expand Down