1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Use a key for main controller

This commit is contained in:
Pierre HUBERT 2020-05-09 14:07:14 +02:00
parent 6d95d5f7a2
commit 4367dcc728
4 changed files with 18 additions and 3 deletions

View File

@ -6,13 +6,21 @@ import 'package:flutter/material.dart';
/// ///
/// @author Pierre Hubert /// @author Pierre Hubert
/// Main controller key statically shared across application
///
/// Do not use it directly to avoid context leak, instead use the access method
/// [MainController.of]
final mainControllerKey = GlobalKey<MainController>();
mixin MainRoute implements StatefulWidget {} mixin MainRoute implements StatefulWidget {}
/// Public interface of home controller /// Public interface of home controller
mixin MainController implements State<MainRoute> { mixin MainController implements State<MainRoute> {
/// Get current instance of Home controller /// Get current instance of Home controller
static MainController of(BuildContext context) => static MainController of(BuildContext context) {
context.findAncestorStateOfType<MainController>(); assert(context != null); // A future implementation might need context again
return mainControllerKey.currentState;
}
/// Open user page /// Open user page
void openUserPage(int userID); void openUserPage(int userID);

View File

@ -27,6 +27,8 @@ import '../login_route.dart';
/// @author Pierre HUBERT /// @author Pierre HUBERT
class SmartphoneMainRoute extends StatefulWidget implements MainRoute { class SmartphoneMainRoute extends StatefulWidget implements MainRoute {
const SmartphoneMainRoute({Key key}) : super(key: key);
@override @override
State<StatefulWidget> createState() => _MainRouteState(); State<StatefulWidget> createState() => _MainRouteState();
} }

View File

@ -11,6 +11,8 @@ import 'package:flutter/material.dart';
/// @author Pierre Hubert /// @author Pierre Hubert
class TabletRoute extends StatefulWidget implements MainRoute { class TabletRoute extends StatefulWidget implements MainRoute {
const TabletRoute({Key key}) : super(key: key);
@override @override
_TabletRouteState createState() => _TabletRouteState(); _TabletRouteState createState() => _TabletRouteState();
} }

View File

@ -1,6 +1,7 @@
import 'package:comunic/helpers/events_helper.dart'; import 'package:comunic/helpers/events_helper.dart';
import 'package:comunic/helpers/websocket_helper.dart'; import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/routes/login_route.dart';
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/routes/main_route/smartphone_route.dart'; import 'package:comunic/ui/routes/main_route/smartphone_route.dart';
import 'package:comunic/ui/routes/main_route/tablet_route.dart'; import 'package:comunic/ui/routes/main_route/tablet_route.dart';
import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/ui/widgets/safe_state.dart';
@ -77,7 +78,9 @@ class _InitializeWidgetState extends SafeState<InitializeWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return !_error && WebSocketHelper.isConnected() return !_error && WebSocketHelper.isConnected()
? (isTablet(context) ? TabletRoute() : SmartphoneMainRoute()) ? (isTablet(context)
? TabletRoute(key: mainControllerKey)
: SmartphoneMainRoute(key: mainControllerKey))
: _buildNonReadyWidget(); : _buildNonReadyWidget();
} }