import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; import 'segmented_bar_overflow.dart'; /// The identity of each of a bar's [actions]: its own key, or its type and /// place among actions of that type, within [group] (whoever supplied the /// actions). An action keeps its identity, and so its entrance, while the /// bar rebuilds; a new [group] makes every action new. A /// [SegmentedOverflowAction] is known by the action it wraps. List segmentedActionIds(List actions, Object? group) { final seen = {}; return [ for (final action in actions) if ((action is SegmentedOverflowAction && action.key == null ? action.child : action) case final identity) ( 'segmented.action', group, identity.key ?? ( identity.runtimeType, seen.update( identity.runtimeType, (n) => n + 1, ifAbsent: () => 0, ), ), ), ]; } /// When an action of a bar pops in: [play] false keeps it still (it is /// already there, or it is the whole pill and pops with it). typedef SegmentedActionEntrance = ({bool play, Duration delay}); /// When each piece of one build of a bar pops in. class SegmentedEntrancePlan { const SegmentedEntrancePlan({ required this.leading, required this.title, required this.pill, required this.actions, }); final Duration leading; final Duration title; /// The actions pill's delay (it only pops when it is new). final Duration pill; final List actions; } /// Decides, build by build, when the pieces of a bar pop in with the app's /// shared pop (`PopEntrance`), [Motion.tabEntranceStagger] apart. /// /// On the bar's first frame its pieces enter one after another from the /// leading edge: Back, the title, the actions pill, then each action in it. /// A piece that turns up later enters at once; actions arriving together /// (with a new pill, or a new screen's set) still follow one another. A lone /// action is the whole pill, so it pops with its pill and not again. A piece /// that stays never pops again: the plan only times newcomers, and each /// entrance decides once, when it is first built. class SegmentedEntrancePlanner { bool _shown = false; bool _hadActions = false; Set _actionIds = const {}; /// Called once the bar's first frame is drawn. void markShown() => _shown = true; SegmentedEntrancePlan plan({ required bool leading, required bool title, required List actionIds, }) { var order = 0; Duration next() => Motion.tabEntranceStagger * order++; Duration piece() => _shown ? Duration.zero : next(); final leadingDelay = leading ? piece() : Duration.zero; final titleDelay = title ? piece() : Duration.zero; final freshPill = actionIds.isNotEmpty && !_hadActions; final pillDelay = freshPill ? next() : Duration.zero; final lone = actionIds.length == 1; final actions = [ for (final id in actionIds) if (_actionIds.contains(id) || (freshPill && lone)) (play: false, delay: Duration.zero) else (play: true, delay: next()), ]; _hadActions = actionIds.isNotEmpty; _actionIds = actionIds.toSet(); return SegmentedEntrancePlan( leading: leadingDelay, title: titleDelay, pill: pillDelay, actions: actions, ); } }