Skip to content

Commit

Permalink
Merge pull request #665 from Flutterando/4.5.0
Browse files Browse the repository at this point in the history
4.5.0
  • Loading branch information
jacobaraujo7 authored Feb 22, 2022
2 parents 269ed03 + 174bd72 commit e7cdfdb
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 39 deletions.
6 changes: 4 additions & 2 deletions doc/docs/flutter_modular/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
13 changes: 9 additions & 4 deletions doc/docs/flutter_modular/navegation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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,
);
}
}
Expand Down
20 changes: 17 additions & 3 deletions doc/docs/flutter_modular/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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.
9 changes: 6 additions & 3 deletions doc/docs/flutter_modular/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
Expand Down
10 changes: 8 additions & 2 deletions flutter_modular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [4.4.1] - 2022-02-18
## [4.5.0] - 2022-02-22

- @Deprecated: `.modular()` extension.
Use instead:
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions flutter_modular/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 6 additions & 18 deletions flutter_modular/lib/flutter_modular.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,9 +51,6 @@ void cleanGlobals() {
cleanInjector();
}

@visibleForTesting
String initialRouteDeclaredInMaterialApp = '/';

extension ModularExtensionMaterial on MaterialApp {
///Use instead:
///```dart
Expand All @@ -66,13 +62,9 @@ extension ModularExtensionMaterial on MaterialApp {
///```
@Deprecated('Use **MaterialApp.router** instead')
MaterialApp modular() {
injector
.get<IModularNavigator>()
.setObserver(navigatorObservers ?? <NavigatorObserver>[]);

injector.get<IModularNavigator>().setNavigatorKey(navigatorKey);

initialRouteDeclaredInMaterialApp = initialRoute ?? '/';
Modular.setObservers(navigatorObservers ?? []);
Modular.setNavigatorKey(navigatorKey);
Modular.setInitialRoute(initialRoute ?? '/');

final app = MaterialApp.router(
key: key,
Expand Down Expand Up @@ -121,16 +113,12 @@ extension ModularExtensionCupertino on CupertinoApp {
///```
@Deprecated('Use CupertinoApp.router instead')
CupertinoApp modular() {
injector
.get<IModularNavigator>()
.setObserver(navigatorObservers ?? <NavigatorObserver>[]);

injector.get<IModularNavigator>().setNavigatorKey(navigatorKey);
Modular.setObservers(navigatorObservers ?? []);
Modular.setNavigatorKey(navigatorKey);
Modular.setInitialRoute(initialRoute ?? '/');

(injector.get<IModularBase>() as ModularBase).flags.isCupertino = true;

initialRouteDeclaredInMaterialApp = initialRoute ?? '/';

final app = CupertinoApp.router(
key: key,
routeInformationProvider: routeInformationProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract class IModularNavigator implements Listenable {
/// ```
void navigate(String path, {dynamic arguments});

void setObserver(List<NavigatorObserver> navigatorObservers);
void setObservers(List<NavigatorObserver> navigatorObservers);

void setNavigatorKey(GlobalKey<NavigatorState>? navigatorkey);
}
35 changes: 34 additions & 1 deletion flutter_modular/lib/src/presenter/modular_base.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<NavigatorObserver> navigatorObservers);

/// Change the navigatorKey
void setNavigatorKey(GlobalKey<NavigatorState>? key);

@visibleForTesting
String get initialRoutePath;
}

class ModularBase implements IModularBase {
Expand All @@ -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,
Expand Down Expand Up @@ -190,4 +208,19 @@ class ModularBase implements IModularBase {
void reassemble() {
reassembleTracker();
}

@override
void setInitialRoute(String value) {
_initialRoutePath = value;
}

@override
void setNavigatorKey(GlobalKey<NavigatorState>? key) {
routerDelegate.setNavigatorKey(key);
}

@override
void setObservers(List<NavigatorObserver> navigatorObservers) {
routerDelegate.setObservers(navigatorObservers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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!;
}

_firstParse = true;
} else {
// ignore: invalid_use_of_visible_for_testing_member
path = routeInformation.location ?? initialRouteDeclaredInMaterialApp;
path = routeInformation.location ?? Modular.initialRoutePath;
}

return await selectBook(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ModularRouterDelegate extends RouterDelegate<ModularBook>
}

@override
void setObserver(List<NavigatorObserver> navigatorObservers) {
void setObservers(List<NavigatorObserver> navigatorObservers) {
observers = navigatorObservers;
notifyListeners();
}
Expand Down
2 changes: 1 addition & 1 deletion flutter_modular/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void main() {
});

test('setObserver', () {
delegate.setObserver([NavigatorObserver()]);
delegate.setObservers([NavigatorObserver()]);
expect(delegate.observers.length, 1);
});

Expand Down

0 comments on commit e7cdfdb

Please sign in to comment.