Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added factory receiving a widget #136

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
3 changes: 1 addition & 2 deletions lib/widgets/story_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

import '../utils.dart';
Expand Down Expand Up @@ -46,7 +45,7 @@ class ImageLoader {

this.state = LoadState.success;

PaintingBinding.instance!.instantiateImageCodec(imageBytes).then(
PaintingBinding.instance.instantiateImageCodec(imageBytes).then(
(codec) {
this.frames = codec;
onComplete();
Expand Down
78 changes: 68 additions & 10 deletions lib/widgets/story_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'dart:ui';

import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -161,6 +160,22 @@ class StoryItem {
);
}

/// Factory constructor for custom widgets. [controller] should be same instance as
/// one passed to the `StoryView`
factory StoryItem.widget({
required Widget widget,
required StoryController controller,
Key? key,
bool shown = false,
Duration? duration,
}) {
return StoryItem(
widget,
shown: shown,
duration: duration ?? Duration(seconds: 3),
);
}

/// Shorthand for creating inline image. [controller] should be same instance as
/// one passed to the `StoryView`
factory StoryItem.inlineImage({
Expand Down Expand Up @@ -378,6 +393,12 @@ class StoryView extends StatefulWidget {
/// The pages to displayed.
final List<StoryItem?> storyItems;

/// Show or not story progress indicator.
final bool? showStoryProgressIndicator;

/// Define current story progress color
final Color? activeStoryProgressColor;

/// Callback for when a full cycle of story is shown. This will be called
/// each time the full story completes when [repeat] is set to `true`.
final VoidCallback? onComplete;
Expand Down Expand Up @@ -405,15 +426,20 @@ class StoryView extends StatefulWidget {
// Controls the playback of the stories
final StoryController controller;

final bool Function()? canGoToNext;

StoryView({
required this.storyItems,
required this.controller,
this.showStoryProgressIndicator,
this.onComplete,
this.onStoryShow,
this.progressPosition = ProgressPosition.top,
this.repeat = false,
this.inline = false,
this.onVerticalSwipeComplete,
this.activeStoryProgressColor,
this.canGoToNext,
});

@override
Expand Down Expand Up @@ -473,8 +499,15 @@ class StoryViewState extends State<StoryView> with TickerProviderStateMixin {
break;

case PlaybackState.next:
_removeNextHold();
_goForward();
final result = widget.canGoToNext?.call() ?? true;

if (result) {
_removeNextHold();
_goForward();
} else {
widget.controller.play();
}

break;

case PlaybackState.previous:
Expand Down Expand Up @@ -639,6 +672,8 @@ class StoryViewState extends State<StoryView> with TickerProviderStateMixin {
indicatorHeight: widget.inline
? IndicatorHeight.small
: IndicatorHeight.large,
showStoryProgressIndicator: widget.showStoryProgressIndicator,
activeStoryProgressColor: widget.activeStoryProgressColor,
),
),
),
Expand Down Expand Up @@ -726,11 +761,15 @@ class PageBar extends StatefulWidget {
final List<PageData> pages;
final Animation<double>? animation;
final IndicatorHeight indicatorHeight;
final bool? showStoryProgressIndicator;
final Color? activeStoryProgressColor;

PageBar(
this.pages,
this.animation, {
this.indicatorHeight = IndicatorHeight.large,
this.showStoryProgressIndicator,
this.activeStoryProgressColor,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -774,11 +813,17 @@ class PageBarState extends State<PageBar> {
child: Container(
padding: EdgeInsets.only(
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 : 3,
),
child: widget.showStoryProgressIndicator ?? false
? SizedBox()
: StoryProgressIndicator(
isPlaying(it)
? widget.animation!.value
: (it.shown ? 1 : 0),
indicatorHeight:
widget.indicatorHeight == IndicatorHeight.large ? 5 : 3,
activeStoryProgressColor: widget.activeStoryProgressColor,
isCurrent: isPlaying(it),
),
),
);
}).toList(),
Expand All @@ -792,10 +837,15 @@ class StoryProgressIndicator extends StatelessWidget {
/// From `0.0` to `1.0`, determines the progress of the indicator
final double value;
final double indicatorHeight;
final Color? activeStoryProgressColor, backgroundStoryProgressColor;
final bool isCurrent;

StoryProgressIndicator(
this.value, {
this.indicatorHeight = 5,
this.activeStoryProgressColor,
this.backgroundStoryProgressColor,
required this.isCurrent,
});

@override
Expand All @@ -805,11 +855,19 @@ class StoryProgressIndicator extends StatelessWidget {
this.indicatorHeight,
),
foregroundPainter: IndicatorOval(
Colors.white.withOpacity(0.8),
isCurrent
? activeStoryProgressColor == null
? Colors.white.withOpacity(0.8)
: activeStoryProgressColor!.withOpacity(0.8)
: backgroundStoryProgressColor == null
? Colors.white.withOpacity(0.8)
: backgroundStoryProgressColor!.withOpacity(0.8),
this.value,
),
painter: IndicatorOval(
Colors.white.withOpacity(0.4),
backgroundStoryProgressColor == null
? Colors.white.withOpacity(0.4)
: backgroundStoryProgressColor!.withOpacity(.4),
1.0,
),
);
Expand Down