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
2019-07-01 11:57:34 +02:00

114 lines
2.8 KiB
Dart

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<StatefulWidget> createState() => _HomeRouteState();
}
class _HomeRouteState extends State<HomeRoute> {
BarCallbackActions _currTab = BarCallbackActions.OPEN_CONVERSATIONS;
List<BarCallbackActions> history = List();
/// Change currently visible tab
void _changeTab(BarCallbackActions newTab) {
setState(() {
_currTab = newTab;
});
}
/// 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(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<void> _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();
}));
}
}