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 : /// Create transition with builder. 18 0 : TransitionContainer( 19 : {this.builder, this.duration, this.disableAnimateWhenUpdate}) 20 0 : : assert(builder != null); 21 : 22 : /// Wrap a widget with scale transition. 23 1 : TransitionContainer.scale( 24 : {Widget child, Curve curve, this.duration, this.disableAnimateWhenUpdate}) 25 1 : : builder = ScaleBuilder(curve: curve, child: child); 26 : 27 : /// Wrap a widget with slide transition. 28 1 : TransitionContainer.slide({ 29 : Widget child, 30 : Curve curve, 31 : this.duration, 32 : bool reverse = false, 33 : this.disableAnimateWhenUpdate, 34 1 : }) : builder = SlideBuilder(curve: curve, child: child, reverse: reverse); 35 : 36 : /// Wrap a widget with flip transition. 37 1 : TransitionContainer.flip({ 38 : Widget topChild, 39 : Widget bottomChild, 40 : Curve curve, 41 : double height, 42 : this.duration, 43 : this.disableAnimateWhenUpdate, 44 1 : }) : builder = FlipBuilder( 45 : height, 46 : curve: curve, 47 : topChild: topChild, 48 : bottomChild: bottomChild, 49 : ); 50 : 51 1 : @override 52 : _State createState() { 53 1 : return _State(); 54 : } 55 : } 56 : 57 : class _State extends State<TransitionContainer> with TickerProviderStateMixin { 58 : AnimationController animationController; 59 : Animation animation; 60 : 61 1 : @override 62 : void initState() { 63 1 : super.initState(); 64 1 : _setAnimation(); 65 : } 66 : 67 1 : void _setAnimation() { 68 2 : animationController = AnimationController( 69 : vsync: this, 70 3 : duration: widget.duration ?? Duration(milliseconds: 150), 71 4 : )..addListener(() => setState(() {})); 72 2 : animationController.forward(); 73 5 : animation = widget.builder.animation(animationController); 74 : } 75 : 76 1 : @override 77 : void didUpdateWidget(TransitionContainer oldWidget) { 78 1 : super.didUpdateWidget(oldWidget); 79 6 : if (oldWidget.builder.runtimeType != widget.builder.runtimeType) { 80 2 : animationController?.dispose(); 81 1 : _setAnimation(); 82 : } else { 83 3 : if (widget.disableAnimateWhenUpdate == true) { 84 : return; 85 : } 86 2 : animationController?.reset(); 87 2 : animationController?.forward(); 88 : } 89 : } 90 : 91 1 : @override 92 : void dispose() { 93 2 : animationController?.dispose(); 94 1 : super.dispose(); 95 : } 96 : 97 1 : @override 98 : Widget build(BuildContext context) { 99 4 : return widget.builder.build(animation); 100 : } 101 : }