|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +enum WidgetMarker { graph, stats, info } |
| 4 | + |
| 5 | +class WidgetSwitchDemo extends StatelessWidget { |
| 6 | + @override |
| 7 | + Widget build(BuildContext context) { |
| 8 | + return MaterialApp( |
| 9 | + home: Scaffold( |
| 10 | + appBar: AppBar( |
| 11 | + title: Text("WidgetSwitchDemo"), |
| 12 | + ), |
| 13 | + body: BodyWidget(), |
| 14 | + ), |
| 15 | + ); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class BodyWidget extends StatefulWidget { |
| 20 | + @override |
| 21 | + State<StatefulWidget> createState() => BodyWidgetState(); |
| 22 | +} |
| 23 | + |
| 24 | +class BodyWidgetState extends State<BodyWidget> with SingleTickerProviderStateMixin<BodyWidget> { |
| 25 | + WidgetMarker selectedWidgetMarker = WidgetMarker.graph; |
| 26 | + AnimationController _controller; |
| 27 | + Animation _animation; |
| 28 | + |
| 29 | + @override |
| 30 | + void initState() { |
| 31 | + super.initState(); |
| 32 | + _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 500)); |
| 33 | + _animation = Tween(begin: 0.0, end: 1.0).animate(_controller); |
| 34 | + } |
| 35 | + |
| 36 | + @override |
| 37 | + void dispose() { |
| 38 | + super.dispose(); |
| 39 | + _controller.dispose(); |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + return Column( |
| 45 | + children: <Widget>[ |
| 46 | + Row( |
| 47 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 48 | + children: <Widget>[ |
| 49 | + FlatButton( |
| 50 | + onPressed: () { |
| 51 | + setState(() { |
| 52 | + selectedWidgetMarker = WidgetMarker.graph; |
| 53 | + }); |
| 54 | + }, |
| 55 | + child: Text("Graph", style: TextStyle(color: (selectedWidgetMarker == WidgetMarker.graph) ? Colors.black : Colors.black12),), |
| 56 | + ), |
| 57 | + FlatButton( |
| 58 | + onPressed: () { |
| 59 | + setState(() { |
| 60 | + selectedWidgetMarker = WidgetMarker.stats; |
| 61 | + }); |
| 62 | + }, |
| 63 | + child: Text("Stats", style: TextStyle(color: (selectedWidgetMarker == WidgetMarker.stats) ? Colors.black : Colors.black12)), |
| 64 | + ), |
| 65 | + FlatButton( |
| 66 | + onPressed: () { |
| 67 | + setState(() { |
| 68 | + selectedWidgetMarker = WidgetMarker.info; |
| 69 | + }); |
| 70 | + }, |
| 71 | + child: Text("Info", style: TextStyle(color: (selectedWidgetMarker == WidgetMarker.info) ? Colors.black : Colors.black12)), |
| 72 | + ), |
| 73 | + ], |
| 74 | + ), |
| 75 | + FutureBuilder( |
| 76 | + future: _playAnimation(), |
| 77 | + builder: (BuildContext context, AsyncSnapshot snapshot) { |
| 78 | + return getCustomContainer(); |
| 79 | + }, |
| 80 | + ) |
| 81 | + ], |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + _playAnimation() { |
| 86 | + _controller.reset(); |
| 87 | + _controller.forward(); |
| 88 | + } |
| 89 | + |
| 90 | + Widget getCustomContainer() { |
| 91 | + switch (selectedWidgetMarker) { |
| 92 | + case WidgetMarker.graph: |
| 93 | + return getGraphContainer(); |
| 94 | + case WidgetMarker.stats: |
| 95 | + return getStatsContainer(); |
| 96 | + case WidgetMarker.info: |
| 97 | + return getInfoContainer(); |
| 98 | + } |
| 99 | + return getGraphContainer(); |
| 100 | + } |
| 101 | + |
| 102 | + Widget getGraphContainer() { |
| 103 | + return FadeTransition( |
| 104 | + opacity: _animation, |
| 105 | + child: Container( |
| 106 | + color: Colors.red, |
| 107 | + height: 200, |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + Widget getStatsContainer() { |
| 113 | + return FadeTransition( |
| 114 | + opacity: _animation, |
| 115 | + child: Container( |
| 116 | + color: Colors.green, |
| 117 | + height: 300, |
| 118 | + ), |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + Widget getInfoContainer() { |
| 123 | + return FadeTransition( |
| 124 | + opacity: _animation, |
| 125 | + child: Container( |
| 126 | + color: Colors.blue, |
| 127 | + height: 400, |
| 128 | + ), |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments