import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/ui/screens/conversations_list_screen.dart'; import 'package:comunic/ui/screens/friends_list_screen.dart'; import 'package:comunic/ui/screens/newest_posts.dart'; import 'package:comunic/ui/widgets/navbar_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; import 'login_route.dart'; /// Main route of the application /// /// @author Pierre HUBERT class HomeRoute extends StatefulWidget { @override State createState() => _HomeRouteState(); } class _HomeRouteState extends State { BarCallbackActions _currTab = BarCallbackActions.OPEN_CONVERSATIONS; List history = List(); /// Change currently visible tab void _changeTab(BarCallbackActions newTab) { setState(() { _currTab = newTab; }); } /// 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(BarCallbackActions action) { /// Check more quick actions switch (action) { case BarCallbackActions.ACTION_LOGOUT: _logoutRequested(); break; default: if (_currTab != action) history.add(action); _changeTab(action); } } @override void initState() { super.initState(); history.add(_currTab); } /// Build the body of the application Widget _buildBody(BuildContext context) { switch (_currTab) { case BarCallbackActions.OPEN_CONVERSATIONS: return ConversationsListScreen(); case BarCallbackActions.OPEN_NEWEST_POSTS: return NewestPostsScreen(); case BarCallbackActions.OPEN_FRIENDS: return FriendsListScreen(); default: throw "Invalid tab : " + _currTab.toString(); } } @override Widget build(BuildContext context) { return Container( color: Colors.blueAccent, child: SafeArea( // Avoid OS areas child: WillPopScope( onWillPop: _willPop, child: Scaffold( appBar: ComunicAppBar( onTap: _onTap, selectedAction: _currTab, ), body: SafeArea( child: _buildBody(context), ), ), ), ), ); } /// Handle logout requests from user Future _logoutRequested() async { if (!await showConfirmDialog( context: context, message: tr("Do you really want to sign out from the application ?"), title: tr("Sign out"))) return; await AccountHelper().signOut(); Navigator.pushReplacement(context, MaterialPageRoute(builder: (c){ return LoginRoute(); })); } }