Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.44 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import './destination.dart';
  3.  
  4. class HomePage extends StatefulWidget {
  5.   @override
  6.   _HomePageState createState() => _HomePageState();
  7. }
  8.  
  9. class _HomePageState extends State<HomePage> with TickerProviderStateMixin<HomePage> {
  10.   List<Key> _destinationKeys;
  11.   List<AnimationController> _faders;
  12.   int _currentIndex = 0;
  13.  
  14.   @override
  15.   void initState() {
  16.     super.initState();
  17.  
  18.     _faders = allDestinations.map<AnimationController>((Destination destination) {
  19.       return AnimationController(vsync: this, duration: Duration(milliseconds: 200));
  20.     }).toList();
  21.     _faders[_currentIndex].value = 1.0;
  22.     _destinationKeys = List<Key>.generate(allDestinations.length, (int index) => GlobalKey()).toList();
  23.   }
  24.  
  25.   @override
  26.   void dispose() {
  27.     for (AnimationController controller in _faders)
  28.       controller.dispose();
  29.     super.dispose();
  30.   }
  31.  
  32.   @override
  33.   Widget build(BuildContext context) {
  34.     return Scaffold(
  35.       body: SafeArea(
  36.         top: false,
  37.         child: Stack(
  38.           fit: StackFit.expand,
  39.           children: allDestinations.map((Destination destination) {
  40.             final Widget view = FadeTransition(
  41.               opacity: _faders[destination.index].drive(CurveTween(curve: Curves.fastOutSlowIn)),
  42.               child: KeyedSubtree(
  43.                 key: _destinationKeys[destination.index],
  44.                 child: DestinationView(
  45.                   destination: destination,
  46.                 ),
  47.               ),
  48.             );
  49.             if (destination.index == _currentIndex) {
  50.               _faders[destination.index].forward();
  51.               return view;
  52.             } else {
  53.               _faders[destination.index].reverse();
  54.               if (_faders[destination.index].isAnimating) {
  55.                 return IgnorePointer(child: view);
  56.               }
  57.               return Offstage(child: view);
  58.             }
  59.           }).toList(),
  60.         ),
  61.       ),
  62.       bottomNavigationBar: BottomNavigationBar(
  63.         currentIndex: _currentIndex,
  64.         onTap: (int index) {
  65.           setState(() {
  66.             _currentIndex = index;
  67.           });
  68.         },
  69.         items: allDestinations.map((Destination destination) {
  70.           return BottomNavigationBarItem(
  71.             icon: Icon(destination.icon),
  72.             backgroundColor: destination.color,
  73.             title: Text(destination.title)
  74.           );
  75.         }).toList(),
  76.       ),
  77.     );
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement