From 1ecae354174967434f82e7225696398f1a744806 Mon Sep 17 00:00:00 2001 From: Jacob Moura Date: Tue, 22 Feb 2022 00:00:57 -0300 Subject: [PATCH 1/3] Added new initial commands doc --- doc/docs/flutter_modular/module.md | 6 ++++-- doc/docs/flutter_modular/navegation.md | 13 +++++++++---- doc/docs/flutter_modular/start.md | 20 +++++++++++++++++--- doc/docs/flutter_modular/widgets.md | 9 ++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/doc/docs/flutter_modular/module.md b/doc/docs/flutter_modular/module.md index 50a1db38..bf874282 100644 --- a/doc/docs/flutter_modular/module.md +++ b/doc/docs/flutter_modular/module.md @@ -32,10 +32,12 @@ void main(){ class AppWidget extends StatelessWidget { Widget build(BuildContext context){ - return MaterialApp( + return MaterialApp.router( title: 'My Smart App', theme: ThemeData(primarySwatch: Colors.blue), - ).modular(); //added by extension + routeInformationParser: Modular.routeInformationParser, + routerDelegate: Modular.routerDelegate, + ); //added by extension } } diff --git a/doc/docs/flutter_modular/navegation.md b/doc/docs/flutter_modular/navegation.md index 2b709930..737640db 100644 --- a/doc/docs/flutter_modular/navegation.md +++ b/doc/docs/flutter_modular/navegation.md @@ -25,7 +25,9 @@ class AppWidget extends StatelessWidget { return MaterialApp( title: 'My Smart App', theme: ThemeData(primarySwatch: Colors.blue), - ).modular(); //added by extension + routeInformationParser: Modular.routeInformationParser, + routerDelegate: Modular.routerDelegate, + ); //added by extension } } @@ -285,11 +287,14 @@ void main() { class AppWidget extends StatelessWidget { Widget build(BuildContext context) { - return MaterialApp( + Modular.setInitialRoute('/page1'); + + return MaterialApp.router( title: 'My Smart App', theme: ThemeData(primarySwatch: Colors.blue), - initialRoute: '/page1', - ).modular(); + routeInformationParser: Modular.routeInformationParser, + routerDelegate: Modular.routerDelegate, + ); } } diff --git a/doc/docs/flutter_modular/start.md b/doc/docs/flutter_modular/start.md index e2396a89..daea5144 100644 --- a/doc/docs/flutter_modular/start.md +++ b/doc/docs/flutter_modular/start.md @@ -144,7 +144,7 @@ The main Widget's function is to instantiate the MaterialApp or CupertinoApp. In these main Widgets it's also necessary to set the custom route system. For this next snippet we'll use **MaterialApp**, but the process is exactly the same for CupertinoApp. -```dart title="lib/main.dart" {8-15} +```dart title="lib/main.dart" {9-16} import 'package:flutter/material.dart'; import 'package:flutter_modular/flutter_modular.dart'; @@ -154,7 +154,7 @@ void main(){ class AppWidget extends StatelessWidget { Widget build(BuildContext context){ - return MaterialApp( + return MaterialApp.router( title: 'My Smart App', theme: ThemeData(primarySwatch: Colors.blue), routeInformationParser: Modular.routeInformationParser, @@ -185,6 +185,20 @@ class HomePage extends StatelessWidget { } ``` -Here we create a Widget called **AppWidget** containing an instance of **MaterialApp**. Note that in the end, we call **.modular()** method that was added to **MaterialApp** through an extension. +Here we create a Widget called **AppWidget** containing an instance of **MaterialApp.router**. + + +## Support methods + +Navigator 2.0 made Flutter's routing system more dynamic, but some information, previously passed in MaterialApp or CupertinoApp, has been removed, and it will be necessary to configure it using Modular's own support methods. + +```dart +Modular.setNavigatorKey(myNavigatorKey); + +Modular.setObservers([myObserver]); + +Modular.setInitialRoute('/home'); +``` + That's enough to run a Modular app. In the next steps let's explore navigation. diff --git a/doc/docs/flutter_modular/widgets.md b/doc/docs/flutter_modular/widgets.md index 0bfad906..d2dd7ef2 100644 --- a/doc/docs/flutter_modular/widgets.md +++ b/doc/docs/flutter_modular/widgets.md @@ -66,11 +66,14 @@ void main() { class AppWidget extends StatelessWidget { Widget build(BuildContext context) { - return MaterialApp( + Modular.setInitialRoute('/page1'); + + return MaterialApp.router( title: 'My Smart App', theme: ThemeData(primarySwatch: Colors.blue), - initialRoute: '/page1', - ).modular(); + routeInformationParser: Modular.routeInformationParser, + routerDelegate: Modular.routerDelegate, + ); } } From 5aa062e47976fde39acc56538b26169e2bd6353c Mon Sep 17 00:00:00 2001 From: Jacob Moura Date: Tue, 22 Feb 2022 00:07:52 -0300 Subject: [PATCH 2/3] added setNavigatorKey setObservers setInitialRoute --- flutter_modular/analysis_options.yaml | 1 + flutter_modular/lib/flutter_modular.dart | 24 ++++--------- .../presenter/models/modular_navigator.dart | 2 +- .../lib/src/presenter/modular_base.dart | 35 ++++++++++++++++++- .../modular_route_information_parser.dart | 4 +-- .../navigation/modular_router_delegate.dart | 2 +- .../modular_router_delegate_test.dart | 2 +- 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/flutter_modular/analysis_options.yaml b/flutter_modular/analysis_options.yaml index 839cc647..983d2d89 100644 --- a/flutter_modular/analysis_options.yaml +++ b/flutter_modular/analysis_options.yaml @@ -22,6 +22,7 @@ linter: # `// ignore_for_file: name_of_lint` syntax on the line or in the file # producing the lint. rules: + no_logic_in_create_state: false # avoid_print: false # Uncomment to disable the `avoid_print` rule # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule diff --git a/flutter_modular/lib/flutter_modular.dart b/flutter_modular/lib/flutter_modular.dart index 04fdf657..1e6735e1 100644 --- a/flutter_modular/lib/flutter_modular.dart +++ b/flutter_modular/lib/flutter_modular.dart @@ -6,7 +6,6 @@ import 'package:flutter_modular/src/flutter_modular_module.dart'; import 'package:modular_core/modular_core.dart'; import 'src/domain/usecases/get_arguments.dart'; -import 'src/presenter/models/modular_navigator.dart'; import 'src/presenter/modular_base.dart'; import 'src/presenter/navigation/modular_page.dart'; import 'src/presenter/navigation/modular_router_delegate.dart'; @@ -52,9 +51,6 @@ void cleanGlobals() { cleanInjector(); } -@visibleForTesting -String initialRouteDeclaredInMaterialApp = '/'; - extension ModularExtensionMaterial on MaterialApp { ///Use instead: ///```dart @@ -66,13 +62,9 @@ extension ModularExtensionMaterial on MaterialApp { ///``` @Deprecated('Use **MaterialApp.router** instead') MaterialApp modular() { - injector - .get() - .setObserver(navigatorObservers ?? []); - - injector.get().setNavigatorKey(navigatorKey); - - initialRouteDeclaredInMaterialApp = initialRoute ?? '/'; + Modular.setObservers(navigatorObservers ?? []); + Modular.setNavigatorKey(navigatorKey); + Modular.setInitialRoute(initialRoute ?? '/'); final app = MaterialApp.router( key: key, @@ -121,16 +113,12 @@ extension ModularExtensionCupertino on CupertinoApp { ///``` @Deprecated('Use CupertinoApp.router instead') CupertinoApp modular() { - injector - .get() - .setObserver(navigatorObservers ?? []); - - injector.get().setNavigatorKey(navigatorKey); + Modular.setObservers(navigatorObservers ?? []); + Modular.setNavigatorKey(navigatorKey); + Modular.setInitialRoute(initialRoute ?? '/'); (injector.get() as ModularBase).flags.isCupertino = true; - initialRouteDeclaredInMaterialApp = initialRoute ?? '/'; - final app = CupertinoApp.router( key: key, routeInformationProvider: routeInformationProvider, diff --git a/flutter_modular/lib/src/presenter/models/modular_navigator.dart b/flutter_modular/lib/src/presenter/models/modular_navigator.dart index 46c12482..919e190d 100644 --- a/flutter_modular/lib/src/presenter/models/modular_navigator.dart +++ b/flutter_modular/lib/src/presenter/models/modular_navigator.dart @@ -113,7 +113,7 @@ abstract class IModularNavigator implements Listenable { /// ``` void navigate(String path, {dynamic arguments}); - void setObserver(List navigatorObservers); + void setObservers(List navigatorObservers); void setNavigatorKey(GlobalKey? navigatorkey); } diff --git a/flutter_modular/lib/src/presenter/modular_base.dart b/flutter_modular/lib/src/presenter/modular_base.dart index ae3af894..8c013b29 100644 --- a/flutter_modular/lib/src/presenter/modular_base.dart +++ b/flutter_modular/lib/src/presenter/modular_base.dart @@ -1,6 +1,6 @@ import 'dart:async'; -import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; import '../domain/usecases/get_arguments.dart'; import '../domain/usecases/reassemble_tracker.dart'; import 'package:modular_core/modular_core.dart'; @@ -69,6 +69,18 @@ abstract class IModularBase { /// Navigator 2.0 initializator: RouterDelegate ModularRouterDelegate get routerDelegate; + + /// Change the starting route path + void setInitialRoute(String initialRoute); + + /// Change a list of NavigatorObserver objects + void setObservers(List navigatorObservers); + + /// Change the navigatorKey + void setNavigatorKey(GlobalKey? key); + + @visibleForTesting + String get initialRoutePath; } class ModularBase implements IModularBase { @@ -90,6 +102,12 @@ class ModularBase implements IModularBase { bool _moduleHasBeenStarted = false; + String _initialRoutePath = '/'; + + @visibleForTesting + @override + String get initialRoutePath => _initialRoutePath; + ModularBase({ required this.routeInformationParser, required this.routerDelegate, @@ -190,4 +208,19 @@ class ModularBase implements IModularBase { void reassemble() { reassembleTracker(); } + + @override + void setInitialRoute(String value) { + _initialRoutePath = value; + } + + @override + void setNavigatorKey(GlobalKey? key) { + routerDelegate.setNavigatorKey(key); + } + + @override + void setObservers(List navigatorObservers) { + routerDelegate.setObservers(navigatorObservers); + } } diff --git a/flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart b/flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart index 6f7240fd..2703ae4a 100644 --- a/flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart +++ b/flutter_modular/lib/src/presenter/navigation/modular_route_information_parser.dart @@ -35,7 +35,7 @@ class ModularRouteInformationParser if (routeInformation.location == null || routeInformation.location == '/') { // ignore: invalid_use_of_visible_for_testing_member - path = initialRouteDeclaredInMaterialApp; + path = Modular.initialRoutePath; } else { path = routeInformation.location!; } @@ -43,7 +43,7 @@ class ModularRouteInformationParser _firstParse = true; } else { // ignore: invalid_use_of_visible_for_testing_member - path = routeInformation.location ?? initialRouteDeclaredInMaterialApp; + path = routeInformation.location ?? Modular.initialRoutePath; } return await selectBook(path); diff --git a/flutter_modular/lib/src/presenter/navigation/modular_router_delegate.dart b/flutter_modular/lib/src/presenter/navigation/modular_router_delegate.dart index ceeb66d6..9710d52b 100644 --- a/flutter_modular/lib/src/presenter/navigation/modular_router_delegate.dart +++ b/flutter_modular/lib/src/presenter/navigation/modular_router_delegate.dart @@ -51,7 +51,7 @@ class ModularRouterDelegate extends RouterDelegate } @override - void setObserver(List navigatorObservers) { + void setObservers(List navigatorObservers) { observers = navigatorObservers; notifyListeners(); } diff --git a/flutter_modular/test/src/presenter/navigation/modular_router_delegate_test.dart b/flutter_modular/test/src/presenter/navigation/modular_router_delegate_test.dart index 8065d72f..bde89bda 100644 --- a/flutter_modular/test/src/presenter/navigation/modular_router_delegate_test.dart +++ b/flutter_modular/test/src/presenter/navigation/modular_router_delegate_test.dart @@ -58,7 +58,7 @@ void main() { }); test('setObserver', () { - delegate.setObserver([NavigatorObserver()]); + delegate.setObservers([NavigatorObserver()]); expect(delegate.observers.length, 1); }); From 174bd729692c2fb09bd1c20cd0c3b2882bcbfb47 Mon Sep 17 00:00:00 2001 From: Jacob Moura Date: Tue, 22 Feb 2022 00:12:32 -0300 Subject: [PATCH 3/3] change version --- flutter_modular/CHANGELOG.md | 10 ++++++++-- flutter_modular/pubspec.yaml | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/flutter_modular/CHANGELOG.md b/flutter_modular/CHANGELOG.md index 5f2083c4..db218f1c 100644 --- a/flutter_modular/CHANGELOG.md +++ b/flutter_modular/CHANGELOG.md @@ -1,4 +1,4 @@ -## [4.4.1] - 2022-02-18 +## [4.5.0] - 2022-02-22 - @Deprecated: `.modular()` extension. Use instead: @@ -9,7 +9,13 @@ ); ``` -- Fix bugs. +- Added `Modular.setInitialRoute`. +- Added `Modular.setObservers`. +- Added `Modular.setNavigatorKey`. + +## [4.4.1] - 2022-02-18 + +- Fix bugs in lints. ## [4.4.0+1] - 2022-01-22 diff --git a/flutter_modular/pubspec.yaml b/flutter_modular/pubspec.yaml index a50e2c1a..b2894c29 100644 --- a/flutter_modular/pubspec.yaml +++ b/flutter_modular/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_modular description: Smart project structure with dependency injection and route management -version: 4.4.1 +version: 4.5.0 homepage: https://github.com/Flutterando/modular environment: