From 07d668eb5995568fd760522dadc8c427e5e5ae5a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 23 Apr 2019 11:08:38 +0200 Subject: [PATCH] Added bottom navigation to home route --- lib/ui/routes/home_route.dart | 84 ++++++++++++++++++- .../tiles/CustomBottomNavigationBarItem.dart | 13 +++ 2 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 lib/ui/tiles/CustomBottomNavigationBarItem.dart diff --git a/lib/ui/routes/home_route.dart b/lib/ui/routes/home_route.dart index 0ac6b96..ec0280a 100644 --- a/lib/ui/routes/home_route.dart +++ b/lib/ui/routes/home_route.dart @@ -1,12 +1,90 @@ +import 'package:comunic/ui/tiles/CustomBottomNavigationBarItem.dart'; import 'package:flutter/material.dart'; /// Main route of the application /// /// @author Pierre HUBERT -class HomeRoute extends StatelessWidget { +class HomeRoute extends StatefulWidget { + @override + State createState() => _HomeRouteState(); +} + +class _HomeRouteState extends State { + int _currTab = 0; + List history = List(); + + + /// Change currently visible tab + void _changeTab(int newTabNumber) { + setState(() { + _currTab = newTabNumber; + }); + } + + /// Allows to go to previous tab + Future _willPop() async { + if (history.length == 1) return true; + + history.removeLast(); + _changeTab(history[history.length - 1]); + return false; + } + + /// Handles a new tab being tapped + void _onTap(int number, BuildContext context) { + if (_currTab != number) history.add(number); + _changeTab(number); + } + + @override + void initState() { + super.initState(); + history.add(_currTab); + } + + /// Build the body of the application + Widget _buildBody(BuildContext context) { + switch (_currTab) { + case 0: + return Text("Conversations"); + + case 1: + return Text("Menu"); + + default: + throw "Invalid tab number : " + _currTab.toString(); + } + } + + /// Build navigation bar items + List _buildNavigationBarItems() { + return [ + CustomNavigationBarItem( + icon: Icon(Icons.chat), + title: Text("Conversations"), + ), + CustomNavigationBarItem( + icon: Icon(Icons.menu), + title: Text("Menu"), + ), + ]; + } + @override Widget build(BuildContext context) { - return Text("Home route"); + return WillPopScope( + onWillPop: _willPop, + child: Scaffold( + body: SafeArea( + child: _buildBody(context), + ), + bottomNavigationBar: BottomNavigationBar( + currentIndex: _currTab, + items: _buildNavigationBarItems(), + onTap: (i) => _onTap(i, context), + ), + ), + ); } -} \ No newline at end of file +} diff --git a/lib/ui/tiles/CustomBottomNavigationBarItem.dart b/lib/ui/tiles/CustomBottomNavigationBarItem.dart new file mode 100644 index 0000000..b6d496a --- /dev/null +++ b/lib/ui/tiles/CustomBottomNavigationBarItem.dart @@ -0,0 +1,13 @@ +import 'package:flutter/material.dart'; + +/// Custom navigation bar item +/// +/// @author Pierre HUBERT + +class CustomNavigationBarItem extends BottomNavigationBarItem { + const CustomNavigationBarItem({ + @required Widget icon, + Widget title, + bool selected, + }) : super(icon: icon, title: title, backgroundColor: Colors.blue,); +}