Line data Source code
1 : import 'package:flutter/cupertino.dart'; 2 : import 'package:flutter/material.dart'; 3 : 4 : import 'transition_container_builder.dart'; 5 : 6 : /// Add controller with provided transition api, such as [SlideTransition], [ScaleTransition]. 7 : class TransitionContainer extends StatefulWidget { 8 : /// Build transition. 9 : final TransitionContainerBuilder builder; 10 : 11 : /// Transition duration. 12 : final Duration duration; 13 : 14 : /// Control whether the animation should be skipped when widget change. 15 : final bool disableAnimateWhenUpdate; 16 : 17 0 : TransitionContainer( 18 : {this.builder, this.duration, this.disableAnimateWhenUpdate}) 19 0 : : assert(builder != null); 20 : 21 : /// Wrap a widget with scale transition. 22 1 : TransitionContainer.scale( 23 : {Widget child, Curve curve, this.duration, this.disableAnimateWhenUpdate}) 24 1 : : builder = ScaleBuilder(curve: curve, child: child); 25 : 26 : /// Wrap a widget with slide transition. 27 1 : TransitionContainer.slide({ 28 : Widget child, 29 : Curve curve, 30 : this.duration, 31 : bool reverse = false, 32 : this.disableAnimateWhenUpdate, 33 1 : }) : builder = SlideBuilder(curve: curve, child: child, reverse: reverse); 34 : 35 : /// Wrap a widget with flip transition. 36 1 : TransitionContainer.flip({ 37 : Widget topChild, 38 : Widget bottomChild, 39 : Curve curve, 40 : double height, 41 : this.duration, 42 : this.disableAnimateWhenUpdate, 43 1 : }) : builder = FlipBuilder( 44 : height, 45 : curve: curve, 46 : topChild: topChild, 47 : bottomChild: bottomChild, 48 : ); 49 : 50 1 : @override 51 : _State createState() { 52 1 : return _State(); 53 : } 54 : } 55 : 56 : class _State extends State<TransitionContainer> with TickerProviderStateMixin { 57 : AnimationController animationController; 58 : Animation animation; 59 : 60 1 : @override 61 : void initState() { 62 1 : super.initState(); 63 1 : _setAnimation(); 64 : } 65 : 66 1 : void _setAnimation() { 67 2 : animationController = AnimationController( 68 : vsync: this, 69 3 : duration: widget.duration ?? Duration(milliseconds: 150), 70 4 : )..addListener(() => setState(() {})); 71 2 : animationController.forward(); 72 5 : animation = widget.builder.animation(animationController); 73 : } 74 : 75 1 : @override 76 : void didUpdateWidget(TransitionContainer oldWidget) { 77 1 : super.didUpdateWidget(oldWidget); 78 6 : if (oldWidget.builder.runtimeType != widget.builder.runtimeType) { 79 2 : animationController?.dispose(); 80 1 : _setAnimation(); 81 : } else { 82 3 : if (widget.disableAnimateWhenUpdate == true) { 83 : return; 84 : } 85 2 : animationController?.reset(); 86 2 : animationController?.forward(); 87 : } 88 : } 89 : 90 1 : @override 91 : void dispose() { 92 2 : animationController?.dispose(); 93 1 : super.dispose(); 94 : } 95 : 96 1 : @override 97 : Widget build(BuildContext context) { 98 4 : return widget.builder.build(animation); 99 : } 100 : }