Guest User

Untitled

a guest
Apr 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4. MaterialPageRoute.debugEnableFadingRoutes = true; // ignore: deprecated_member_use
  5. runApp(new MyApp());
  6. }
  7.  
  8. class MyApp extends StatelessWidget {
  9. // This widget is the root of your application.
  10. @override
  11. Widget build(BuildContext context) {
  12. return new MaterialApp(
  13. title: 'Reader',
  14. theme: new ThemeData(
  15. // This is the theme of your application.
  16. //
  17. // Try running your application with "flutter run". You'll see the
  18. // application has a blue toolbar. Then, without quitting the app, try
  19. // changing the primarySwatch below to Colors.green and then invoke
  20. // "hot reload" (press "r" in the console where you ran "flutter run",
  21. // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
  22. // counter didn't reset back to zero; the application is not restarted.
  23. primarySwatch: Colors.blue,
  24. ),
  25. home: new BottomNavigationDemo(),
  26. );
  27. }
  28. }
  29.  
  30. class NavigationIconView {
  31. NavigationIconView({
  32. Widget icon,
  33. String title,
  34. Color color,
  35. TickerProvider vsync,
  36. }) : _icon = icon,
  37. _color = color,
  38. _title = title,
  39. item = new BottomNavigationBarItem(
  40. icon: icon,
  41. title: new Text(title),
  42. backgroundColor: color,
  43. ),
  44. controller = new AnimationController(
  45. duration: kThemeAnimationDuration,
  46. vsync: vsync,
  47. ) {
  48. _animation = new CurvedAnimation(
  49. parent: controller,
  50. curve: const Interval(0.5, 1.0, curve: Curves.fastOutSlowIn),
  51. );
  52. }
  53.  
  54. final Widget _icon;
  55. final Color _color;
  56. final String _title;
  57. final BottomNavigationBarItem item;
  58. final AnimationController controller;
  59. CurvedAnimation _animation;
  60.  
  61. FadeTransition transition(BottomNavigationBarType type, BuildContext context) {
  62. return new FadeTransition(
  63. opacity: _animation,
  64. child: new SlideTransition(
  65. position: new Tween<Offset>(
  66. begin: const Offset(0.0, 0.02), // Slightly down.
  67. end: Offset.zero,
  68. ).animate(_animation),
  69. child: new IconTheme(
  70. data: new IconThemeData(
  71. color: _color,
  72. size: 120.0,
  73. ),
  74. child: new Semantics(
  75. label: 'Placeholder for $_title tab',
  76. child: _icon,
  77. ),
  78. ),
  79. ),
  80. );
  81. }
  82. }
  83.  
  84. class CustomIcon extends StatelessWidget {
  85. @override
  86. Widget build(BuildContext context) {
  87. final IconThemeData iconTheme = IconTheme.of(context);
  88. return new Container(
  89. margin: const EdgeInsets.all(4.0),
  90. width: iconTheme.size - 8.0,
  91. height: iconTheme.size - 8.0,
  92. color: iconTheme.color,
  93. );
  94. }
  95. }
  96.  
  97. class BottomNavigationDemo extends StatefulWidget {
  98. static const String routeName = '/material/bottom_navigation';
  99.  
  100. @override
  101. _BottomNavigationDemoState createState() => new _BottomNavigationDemoState();
  102. }
  103.  
  104. class _BottomNavigationDemoState extends State<BottomNavigationDemo>
  105. with TickerProviderStateMixin {
  106. int _currentIndex = 0;
  107. BottomNavigationBarType _type = BottomNavigationBarType.shifting;
  108. List<NavigationIconView> _navigationViews;
  109.  
  110. @override
  111. void initState() {
  112. super.initState();
  113. _navigationViews = <NavigationIconView>[
  114. new NavigationIconView(
  115. icon: const Icon(Icons.rss_feed),
  116. title: 'Unread',
  117. color: Colors.deepPurple,
  118. vsync: this,
  119. ),
  120. new NavigationIconView(
  121. icon: new Icon(Icons.favorite),
  122. title: 'Favorites',
  123. color: Colors.deepOrange,
  124. vsync: this,
  125. ),
  126. new NavigationIconView(
  127. icon: const Icon(Icons.history),
  128. title: 'History',
  129. color: Colors.teal,
  130. vsync: this,
  131. ),
  132. new NavigationIconView(
  133. icon: const Icon(Icons.category),
  134. title: 'Categories',
  135. color: Colors.indigo,
  136. vsync: this,
  137. ),
  138. ];
  139.  
  140. for (NavigationIconView view in _navigationViews)
  141. view.controller.addListener(_rebuild);
  142.  
  143. _navigationViews[_currentIndex].controller.value = 1.0;
  144. }
  145.  
  146. @override
  147. void dispose() {
  148. for (NavigationIconView view in _navigationViews)
  149. view.controller.dispose();
  150. super.dispose();
  151. }
  152.  
  153. void _rebuild() {
  154. setState(() {
  155. // Rebuild in order to animate views.
  156. });
  157. }
  158.  
  159. Widget _buildTransitionsStack() {
  160. final List<FadeTransition> transitions = <FadeTransition>[];
  161.  
  162. for (NavigationIconView view in _navigationViews)
  163. transitions.add(view.transition(_type, context));
  164.  
  165. // We want to have the newly animating (fading in) views on top.
  166. transitions.sort((FadeTransition a, FadeTransition b) {
  167. final Animation<double> aAnimation = a.opacity;
  168. final Animation<double> bAnimation = b.opacity;
  169. final double aValue = aAnimation.value;
  170. final double bValue = bAnimation.value;
  171. return aValue.compareTo(bValue);
  172. });
  173.  
  174. return new Stack(children: transitions);
  175. }
  176.  
  177. @override
  178. Widget build(BuildContext context) {
  179. final BottomNavigationBar botNavBar = new BottomNavigationBar(
  180. items: _navigationViews
  181. .map((NavigationIconView navigationView) => navigationView.item)
  182. .toList(),
  183. currentIndex: _currentIndex,
  184. type: _type,
  185. onTap: (int index) {
  186. setState(() {
  187. _navigationViews[_currentIndex].controller.reverse();
  188. _currentIndex = index;
  189. _navigationViews[_currentIndex].controller.forward();
  190. });
  191. },
  192. );
  193.  
  194. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  195.  
  196. return new Scaffold(
  197. appBar: new AppBar(
  198. title: const Text('Reader'),
  199. actions: <Widget>[
  200. new IconButton(
  201. icon: new Icon(Icons.settings),
  202. tooltip: 'Settings',
  203. onPressed: () {
  204. _scaffoldKey.currentState.showSnackBar(new SnackBar(
  205. content: new Text('Open Settings!'),
  206. ));
  207. },
  208. )
  209. ]
  210. ),
  211. body: new Center(
  212. child: _buildTransitionsStack()
  213. ),
  214. bottomNavigationBar: botNavBar,
  215. );
  216. }
  217. }
Add Comment
Please, Sign In to add comment