1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/routes/home_route.dart

93 lines
2.1 KiB
Dart
Raw Normal View History

2019-04-23 12:35:41 +00:00
import 'package:comunic/ui/screens/conversations_screen.dart';
2019-04-23 09:48:49 +00:00
import 'package:comunic/ui/screens/menus_screen.dart';
2019-04-23 09:08:38 +00:00
import 'package:comunic/ui/tiles/CustomBottomNavigationBarItem.dart';
2019-04-22 17:16:26 +00:00
import 'package:flutter/material.dart';
/// Main route of the application
///
/// @author Pierre HUBERT
2019-04-23 09:08:38 +00:00
class HomeRoute extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HomeRouteState();
}
class _HomeRouteState extends State<HomeRoute> {
int _currTab = 0;
List<int> history = List();
/// Change currently visible tab
void _changeTab(int newTabNumber) {
setState(() {
_currTab = newTabNumber;
});
}
/// Allows to go to previous tab
Future<bool> _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:
2019-04-23 12:35:41 +00:00
return ConversationsScreen();
2019-04-23 09:08:38 +00:00
case 1:
2019-04-23 09:48:49 +00:00
return MenuScreen();
2019-04-23 09:08:38 +00:00
default:
throw "Invalid tab number : " + _currTab.toString();
}
}
/// Build navigation bar items
List<BottomNavigationBarItem> _buildNavigationBarItems() {
return <BottomNavigationBarItem>[
CustomNavigationBarItem(
icon: Icon(Icons.chat),
title: Text("Conversations"),
),
CustomNavigationBarItem(
icon: Icon(Icons.menu),
title: Text("Menu"),
),
];
}
2019-04-22 17:16:26 +00:00
@override
Widget build(BuildContext context) {
2019-04-23 09:08:38 +00:00
return WillPopScope(
onWillPop: _willPop,
child: Scaffold(
body: SafeArea(
child: _buildBody(context),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currTab,
items: _buildNavigationBarItems(),
onTap: (i) => _onTap(i, context),
),
),
);
2019-04-22 17:16:26 +00:00
}
2019-04-23 09:08:38 +00:00
}