|
| 1 | +import 'package:flutter/widgets.dart'; |
| 2 | + |
| 3 | +/// Based on the PageTransitionSwitcher from the animations package, this widget |
| 4 | +/// allows you to transition between an array of widgets using entry and exit |
| 5 | +/// animations whilst maintaining their state. |
| 6 | +class IndexedTransitionSwitcher extends StatefulWidget { |
| 7 | + /// Creates an [IndexedTransitionSwitcher]. |
| 8 | + const IndexedTransitionSwitcher( |
| 9 | + {@required this.index, |
| 10 | + @required this.children, |
| 11 | + @required this.transitionBuilder, |
| 12 | + this.reverse = false, |
| 13 | + this.duration = const Duration(milliseconds: 300)}); |
| 14 | + |
| 15 | + /// The index of the child to show. |
| 16 | + final int index; |
| 17 | + |
| 18 | + /// The widgets to switch between. |
| 19 | + final List<Widget> children; |
| 20 | + |
| 21 | + /// A function to wrap the child in primary and secondary animations. |
| 22 | + /// |
| 23 | + /// When the index changes, the new child will animate in with the primary |
| 24 | + /// animation, and the old widget will animate out with the secondary |
| 25 | + /// animation. |
| 26 | + final Widget Function(Widget child, Animation<double> primaryAnimation, |
| 27 | + Animation<double> secondaryAnimation) transitionBuilder; |
| 28 | + |
| 29 | + /// The duration of the transition. |
| 30 | + final Duration duration; |
| 31 | + |
| 32 | + /// Whether or not the transition should be reversed. |
| 33 | + /// |
| 34 | + /// If true, the new child will animate in behind the oWld child with the |
| 35 | + /// secondary animation running in reverse, whilst the old child animates |
| 36 | + /// out with the primary animation playing in reverse. |
| 37 | + final bool reverse; |
| 38 | + |
| 39 | + @override |
| 40 | + _IndexedTransitionSwitcherState createState() => |
| 41 | + _IndexedTransitionSwitcherState(); |
| 42 | +} |
| 43 | + |
| 44 | +class _IndexedTransitionSwitcherState extends State<IndexedTransitionSwitcher> |
| 45 | + with TickerProviderStateMixin { |
| 46 | + List<_ChildEntry> _childEntries; |
| 47 | + |
| 48 | + @override |
| 49 | + void initState() { |
| 50 | + super.initState(); |
| 51 | + // Create the page entries |
| 52 | + _childEntries = widget.children |
| 53 | + .asMap() |
| 54 | + .entries |
| 55 | + .map((entry) => _createPageEntry(entry.key, entry.value)) |
| 56 | + .toList(); |
| 57 | + } |
| 58 | + |
| 59 | + @override |
| 60 | + void didUpdateWidget(IndexedTransitionSwitcher oldWidget) { |
| 61 | + super.didUpdateWidget(oldWidget); |
| 62 | + // Transition if the index has changed |
| 63 | + if (widget.index != oldWidget.index) { |
| 64 | + var newChild = |
| 65 | + _childEntries.where((entry) => entry.index == widget.index).first; |
| 66 | + var oldChild = |
| 67 | + _childEntries.where((entry) => entry.index == oldWidget.index).first; |
| 68 | + // Animate the children |
| 69 | + if (widget.reverse) { |
| 70 | + // Animate in the new child |
| 71 | + newChild.primaryController.value = 1; |
| 72 | + newChild.secondaryController.reverse(from: 1); |
| 73 | + // Animate out the old child and unstage it when the animation is complete |
| 74 | + oldChild.secondaryController.value = 0; |
| 75 | + oldChild.primaryController |
| 76 | + .reverse(from: 1) |
| 77 | + .then((value) => setState(() { |
| 78 | + oldChild.onStage = false; |
| 79 | + oldChild.primaryController.reset(); |
| 80 | + oldChild.secondaryController.reset(); |
| 81 | + })); |
| 82 | + } else { |
| 83 | + // Animate in the new child |
| 84 | + newChild.secondaryController.value = 0; |
| 85 | + newChild.primaryController.forward(from: 0); |
| 86 | + // Animate out the old child and unstage it when the animation is complete |
| 87 | + oldChild.primaryController.value = 1; |
| 88 | + oldChild.secondaryController.forward().then((value) => setState(() { |
| 89 | + oldChild.onStage = false; |
| 90 | + oldChild.primaryController.reset(); |
| 91 | + oldChild.secondaryController.reset(); |
| 92 | + })); |
| 93 | + } |
| 94 | + // Reorder the stack and set onStage to true for the new child |
| 95 | + _childEntries.remove(newChild); |
| 96 | + _childEntries.remove(oldChild); |
| 97 | + _childEntries |
| 98 | + .addAll(widget.reverse ? [newChild, oldChild] : [oldChild, newChild]); |
| 99 | + newChild.onStage = true; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + _ChildEntry _createPageEntry(int index, Widget child) { |
| 104 | + // Prepare the animation controllers |
| 105 | + final primaryController = AnimationController( |
| 106 | + value: widget.index == index ? 1.0 : 0, |
| 107 | + duration: widget.duration, |
| 108 | + vsync: this, |
| 109 | + ); |
| 110 | + final secondaryController = AnimationController( |
| 111 | + duration: widget.duration, |
| 112 | + vsync: this, |
| 113 | + ); |
| 114 | + // Create the page entry |
| 115 | + return _ChildEntry( |
| 116 | + key: UniqueKey(), |
| 117 | + index: index, |
| 118 | + primaryController: primaryController, |
| 119 | + secondaryController: secondaryController, |
| 120 | + transitionChild: widget.transitionBuilder( |
| 121 | + child, primaryController, secondaryController), |
| 122 | + onStage: widget.index == index); |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + void dispose() { |
| 127 | + // Dispose of the animation controllers |
| 128 | + for (var entry in _childEntries) { |
| 129 | + entry.dispose(); |
| 130 | + } |
| 131 | + super.dispose(); |
| 132 | + } |
| 133 | + |
| 134 | + @override |
| 135 | + Widget build(BuildContext context) => Stack( |
| 136 | + alignment: Alignment.center, |
| 137 | + fit: StackFit.expand, |
| 138 | + children: _childEntries |
| 139 | + .map<Widget>((entry) => Offstage( |
| 140 | + key: entry.key, |
| 141 | + offstage: !entry.onStage, |
| 142 | + child: entry.transitionChild, |
| 143 | + )) |
| 144 | + .toList(), |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +/// Internal representation of a child. |
| 149 | +class _ChildEntry { |
| 150 | + _ChildEntry( |
| 151 | + {@required this.index, |
| 152 | + @required this.key, |
| 153 | + @required this.primaryController, |
| 154 | + @required this.secondaryController, |
| 155 | + @required this.transitionChild, |
| 156 | + @required this.onStage}); |
| 157 | + |
| 158 | + /// The child index. |
| 159 | + final int index; |
| 160 | + |
| 161 | + /// The key to maintain widget state when moving in the tree. |
| 162 | + final Key key; |
| 163 | + |
| 164 | + /// The entry animation controller. |
| 165 | + final AnimationController primaryController; |
| 166 | + |
| 167 | + /// The exit animation controller. |
| 168 | + final AnimationController secondaryController; |
| 169 | + |
| 170 | + /// The child widget wrapped in the transition. |
| 171 | + final Widget transitionChild; |
| 172 | + |
| 173 | + /// Whether or not the child should be rendered. |
| 174 | + bool onStage; |
| 175 | + |
| 176 | + /// Dispose of the animation controllers |
| 177 | + void dispose() { |
| 178 | + primaryController.dispose(); |
| 179 | + secondaryController.dispose(); |
| 180 | + } |
| 181 | +} |
0 commit comments