Skip to content

Add 'viewBuilder' property in StoryItem #223

@Kigstom

Description

@Kigstom

Its possible to add 'viewBuilder' property to the StoryItem?
This could be useful, with some other minor code cleanup, for the animated widgets, custom widgets and other lazy UI setups.

Sample

class StoryItem {
  ...

  /// The page content view for backward compatibility, but optional
  final Widget? view;

  /// The page content builder for lazy loading
  final Widget Function(BuildContext context)? viewBuilder;

  StoryItem({
    required this.index,
    required this.duration,
    this.isShown = false,
    this.view,
    this.viewBuilder,
  }) : assert(view != null || viewBuilder != null, 'Must provide a `view` or a `viewBuilder`!');

  ...
}
class StoryViewState extends State<StoryView> with TickerProviderStateMixin {
  AnimationController? _animationController;
  Animation<double>? _currentAnimation;

  StreamSubscription<PlaybackState>? _playbackSubscription;

  VerticalDragInfo? verticalDragInfo;

  StoryItem? get _currentStory {
    return widget.storyItems.firstWhereOrNull((it) => !it!.isShown);
  }

  Widget get _currentView {
    var item = _currentStory;
    item ??= widget.storyItems.last;

    if (item == null) return SizedBox.shrink();

    // 1. Build the widget ONLY when it's about to be shown
    Widget generatedView = item.viewBuilder != null
        ? item.viewBuilder!(context)
        : (item.view ?? SizedBox.shrink());

    // 2. Wrap it with the Key to guarantee animations play
    return KeyedSubtree(
      key: ObjectKey(item),
      child: generatedView,
    );

    return item.view ?? SizedBox.shrink();
  }

  ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions