4
2
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
shihern 793e57cd66 refactor: move factory constructor to the top 2020-01-27 21:00:12 +08:00
shihern 197e597815 fix: clear navigator when signing out 2020-01-24 14:19:02 +08:00
shihern 8c42665cf3 fix: enable android back button compatibility 2020-01-24 14:08:06 +08:00
5 changed files with 82 additions and 62 deletions

View File

@ -14,6 +14,10 @@ class ConnectionStatus {
/// Stream of current connection status. Only updates when there is a change in connection.
Stream<bool> get connectionChange => _connectionChangeController.stream;
factory ConnectionStatus() {
return _singleton;
}
/// Private internal constructor for the Singleton
ConnectionStatus._internal() {
// Only call the init function when there is a subscriber to the stream
@ -60,8 +64,4 @@ class ConnectionStatus {
void dispose() {
_connectionChangeController.close();
}
factory ConnectionStatus() {
return _singleton;
}
}

View File

@ -3,6 +3,10 @@ import "package:flutter/material.dart";
import "./widgets/home_view.dart";
class ContactTab extends StatefulWidget {
final GlobalKey<NavigatorState> navigatorKey;
ContactTab({@required this.navigatorKey});
@override
State<StatefulWidget> createState() {
return _ContactTabState();
@ -21,6 +25,7 @@ class _ContactTabState extends State<ContactTab> {
@override
Widget build(BuildContext context) {
return Navigator(
key: widget.navigatorKey,
initialRoute: "contact/home",
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;

View File

@ -8,6 +8,10 @@ import "./widgets/new_group_info_view.dart";
import "../../models/user_model.dart";
class ConversationTab extends StatefulWidget {
final GlobalKey<NavigatorState> navigatorKey;
ConversationTab({@required this.navigatorKey});
@override
State<StatefulWidget> createState() {
return _ConversationTabState();
@ -18,6 +22,7 @@ class _ConversationTabState extends State<ConversationTab> {
@override
Widget build(BuildContext context) {
return Navigator(
key: widget.navigatorKey,
initialRoute: "conversation/home",
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;

View File

@ -26,13 +26,18 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
final loginManager = LoginManager();
// Bottom Bar navigation
int _tabNumber = 1;
int _tabIndex = 1;
List<IconData> itemsList = [
Icons.contacts,
Icons.chat,
Icons.settings,
];
TabController controller;
List<GlobalKey<NavigatorState>> navigatorKeys = [
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>(),
GlobalKey<NavigatorState>()
];
// Current conversaton
String currentConversationId = "";
@ -46,8 +51,6 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
@override
initState() {
super.initState();
controller = TabController(vsync: this, length: 3);
controller.index = 1; // Set default page to conversation page
// initialize conversation manager
loginManager.getToken().then((authToken) async {
@ -78,7 +81,6 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
@override
dispose() {
controller.dispose();
super.dispose();
}
@ -108,59 +110,65 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
controller: controller,
children: <Widget>[
ContactTab(),
ConversationTab(),
SettingsTab(toWelcomePage: () {
Navigator.pushNamed(context, '/welcome');
}),
]),
bottomNavigationBar:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
BottomBar(conversationId: currentConversationId),
BottomNavigationBar(
onTap: (int index) {
setState(() {
_tabNumber = index;
controller.index = _tabNumber;
});
},
items: itemsList.map((data) {
return BottomNavigationBarItem(
icon: itemsList[_tabNumber] == data
? ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Theme.of(context).primaryColor,
Theme.of(context).primaryColorDark,
],
);
},
child: Icon(data, size: 25.0),
)
: Icon(data, color: Colors.grey, size: 20),
title: Container(),
);
}).toList()),
Visibility(
visible: _connectionStatusVisible,
child: Container(
padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
width: MediaQuery.of(context).size.width,
alignment: Alignment(0.0, 0.0),
color: _isOnline ? Colors.green : Colors.red,
child: Text(_isOnline ? 'Online' : 'Offline')),
List<Widget> tabsList = [
ContactTab(navigatorKey: navigatorKeys[0]),
ConversationTab(navigatorKey: navigatorKeys[1]),
SettingsTab(
navigatorKey: navigatorKeys[2],
toWelcomePage: () {
Navigator.of(context).pushNamedAndRemoveUntil('/welcome', (Route<dynamic> route) => false);
}),
];
return WillPopScope(
onWillPop: () async => !await navigatorKeys[_tabIndex].currentState.maybePop(),
child: Scaffold(
key: _scaffoldKey,
body: IndexedStack(
index: _tabIndex,
children: tabsList,
),
]),
bottomNavigationBar:
Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
BottomBar(conversationId: currentConversationId),
BottomNavigationBar(
onTap: (int index) {
setState(() {
_tabIndex = index;
});
},
items: itemsList.map((data) {
return BottomNavigationBarItem(
icon: itemsList[_tabIndex] == data
? ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Theme.of(context).primaryColor,
Theme.of(context).primaryColorDark,
],
);
},
child: Icon(data, size: 25.0),
)
: Icon(data, color: Colors.grey, size: 20),
title: Container(),
);
}).toList()),
Visibility(
visible: _connectionStatusVisible,
child: Container(
padding: EdgeInsets.only(top: 5.0, bottom: 5.0),
width: MediaQuery.of(context).size.width,
alignment: Alignment(0.0, 0.0),
color: _isOnline ? Colors.green : Colors.red,
child: Text(_isOnline ? 'Online' : 'Offline')),
),
]),
),
);
}
}

View File

@ -8,8 +8,9 @@ import "./widgets/home_view.dart";
class SettingsTab extends StatefulWidget {
final VoidCallback toWelcomePage;
final GlobalKey<NavigatorState> navigatorKey;
SettingsTab({@required this.toWelcomePage});
SettingsTab({@required this.toWelcomePage, @required this.navigatorKey});
@override
State<StatefulWidget> createState() {
@ -21,6 +22,7 @@ class _SettingsTabState extends State<SettingsTab> {
@override
Widget build(BuildContext context) {
return Navigator(
key: widget.navigatorKey,
initialRoute: "settings/home",
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;