Advertisement
Guest User

Bottom nav

a guest
Jun 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.02 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:math';
  3. import 'package:english_words/english_words.dart';
  4.  
  5. void main() => runApp(new ColorChangerApp(appTitle: 'Frostnight'));
  6.  
  7. class ColorChangerApp extends StatefulWidget {
  8.  
  9.   static final Color bottomNavIconColor = Colors.black54;
  10.   static final double bottomNavIconSize = 96.0;
  11.  
  12.   final TextStyle light16GreyText = TextStyle(
  13.     fontSize: 16.0,
  14.     color: Colors.blueGrey,
  15.     fontWeight: FontWeight.w300
  16.   );
  17.  
  18.   final TextStyle bottomNavTextStyle = TextStyle(
  19.     fontSize: 14.0,
  20.     color: bottomNavIconColor,
  21.     fontWeight: FontWeight.w300
  22.   );
  23.  
  24.   //Colors we can toggle through
  25.   final List<Color> _appBarColors = [
  26.     Colors.red,
  27.     Colors.blue,
  28.     Colors.black,
  29.     Colors.purple,
  30.     Colors.amber
  31.   ];
  32.  
  33.   final String appTitle;
  34.   ColorChangerApp({this.appTitle});
  35.  
  36.   @override
  37.   createState() => new _ColorChangerAppState();
  38. }
  39.  
  40. class _ColorChangerAppState extends State<ColorChangerApp> {
  41.   Color appBarColor;
  42.   WordPair randomWords;
  43.  
  44.   //Returns a random color from the list we have provided
  45.   Color _getRandomColor() {
  46.     Random rand = new Random();
  47.     int colorIndex = rand.nextInt(widget._appBarColors.length);
  48.  
  49.     Color colorSelected = widget._appBarColors[colorIndex];
  50.     return colorSelected;
  51.   }
  52.  
  53.   //Sets the new state/color of the appbars and stuff
  54.   void _updateColorAndWords() {
  55.     // Keep track of last color to ensure we always change the color
  56.     Color lastAppBarColor = appBarColor;
  57.  
  58.     setState(() {
  59.       randomWords = WordPair.random();
  60.       this.appBarColor = this._getRandomColor();
  61.  
  62.       //If the new appbar color is the same as the last one, then try changing it again
  63.       if (appBarColor == lastAppBarColor) {
  64.         this._updateColorAndWords();
  65.       }
  66.     });
  67.   }
  68.  
  69.   //Initial state
  70.   @override
  71.   initState() {
  72.     super.initState();
  73.     this._updateColorAndWords();
  74.   }
  75.  
  76.   @override
  77.   Widget build(BuildContext context) {
  78.     return new MaterialApp(
  79.       debugShowCheckedModeBanner: false,
  80.       title: 'Startup name generator',
  81.       home: new Scaffold(
  82.         appBar: AppBar(
  83.             backgroundColor: this.appBarColor,
  84.             title: Row(
  85.               children: <Widget>[
  86.                 Padding(
  87.                   padding: EdgeInsets.symmetric(horizontal:16.0),
  88.                   child: Icon(Icons.ac_unit, color:Colors.white),
  89.                 ),
  90.                 Text(
  91.                   widget.appTitle,
  92.                   style: TextStyle(color: Colors.white, fontSize: 20.0),
  93.                 )
  94.               ]
  95.             )
  96.         ),
  97.        
  98.         floatingActionButton: FloatingActionButton(
  99.           backgroundColor: this.appBarColor,
  100.           child: Icon(Icons.edit),
  101.           tooltip: 'Change the background color of the button & appbar',
  102.           onPressed: _updateColorAndWords,
  103.         ),
  104.  
  105.         body: Container(
  106.           padding: EdgeInsets.all(24.0),
  107.           child: Center(
  108.             child: Column(
  109.               children: <Widget>[
  110.                 Text(
  111.                   'Some random words that will change every single time you click the button',
  112.                   style: widget.light16GreyText
  113.                 ),
  114.  
  115.                 Padding(
  116.                   padding: EdgeInsets.symmetric(vertical:16.0),
  117.                   child: Text(
  118.                     randomWords.asPascalCase,
  119.                   ),
  120.                 ),
  121.  
  122.               ]
  123.             ),
  124.           ),
  125.         ),
  126.  
  127.         bottomNavigationBar: BottomNavigationBar(
  128.           currentIndex: 1,
  129.           iconSize: ColorChangerApp.bottomNavIconSize,
  130.        
  131.           items: <BottomNavigationBarItem>[
  132.             BottomNavigationBarItem(
  133.               title: Text(
  134.                 'Cloud',
  135.                 style: widget.bottomNavTextStyle,
  136.               ),
  137.               icon: Icon(
  138.                 Icons.cloud,
  139.                 color:ColorChangerApp.bottomNavIconColor,
  140.                
  141.               ),
  142.             ),
  143.             BottomNavigationBarItem(
  144.               title: Text(
  145.                 'Maps',
  146.                 style: widget.bottomNavTextStyle,
  147.               ),
  148.               icon: Icon(
  149.                 Icons.map,
  150.                 color:ColorChangerApp.bottomNavIconColor,
  151.                
  152.               ),
  153.             ),
  154.             BottomNavigationBarItem(
  155.               backgroundColor: Colors.redAccent,
  156.               title: Text(
  157.                 'Chat',
  158.                 style: widget.bottomNavTextStyle,
  159.               ),
  160.               icon: Icon(
  161.                 Icons.message,
  162.                 color:ColorChangerApp.bottomNavIconColor,
  163.                
  164.               ),
  165.             ),
  166.             BottomNavigationBarItem(
  167.               title: Text(
  168.                 'Privacy',
  169.                 style: widget.bottomNavTextStyle,
  170.               ),
  171.               icon: Icon(
  172.                 Icons.lock,
  173.                 color:ColorChangerApp.bottomNavIconColor,
  174.               ),
  175.             ),
  176.           ],
  177.         ),
  178.       ),
  179.     );
  180.   }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement