From 3d1cbf2b5995c64cb818d092d0031be3c9043948 Mon Sep 17 00:00:00 2001 From: Jan Marsh Date: Wed, 14 Sep 2022 17:35:15 +0200 Subject: [PATCH 1/3] routing with go_router --- Makefile | 6 +++- android/app/src/main/AndroidManifest.xml | 9 +++++- ios/Runner/Info.plist | 17 ++++++++++ lib/src/app.dart | 8 +++-- lib/src/core/routing/router.dart | 32 +++++++++++++++++++ .../dashboard/view/dashboard.page.dart | 4 +-- .../dashboard/view/widgets/counter_grid.dart | 4 +-- .../edit/view/edit_counter.page.dart | 13 -------- pubspec.lock | 14 +++++++- pubspec.yaml | 1 + 10 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 lib/src/core/routing/router.dart diff --git a/Makefile b/Makefile index 42e5466..092fe22 100644 --- a/Makefile +++ b/Makefile @@ -94,4 +94,8 @@ packages-upgrade: l10n: flutter gen-l10n appicon: - flutter pub run flutter_launcher_icons:main -f flutter_launcher_icons.yaml \ No newline at end of file + flutter pub run flutter_launcher_icons:main -f flutter_launcher_icons.yaml +deeplink: + @printf "Android:\nadb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d 'https://counter.de/counters/2'" + @printf "\n\n" + @printf "iOS:\nxcrun simctl openurl booted counter:///counters/2" diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 283a60f..4dbe308 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -7,9 +7,16 @@ while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> - + + + + + + + + + FlutterDeepLinkingEnabled + + CFBundleURLTypes + + + CFBundleTypeRole + Editor + CFBundleURLName + counter.de + CFBundleURLSchemes + + de.coodoo.counter + counter + + + diff --git a/lib/src/app.dart b/lib/src/app.dart index f985d20..a494fd0 100644 --- a/lib/src/app.dart +++ b/lib/src/app.dart @@ -1,8 +1,8 @@ +import 'package:counter_workshop/src/core/routing/router.dart'; import 'package:counter_workshop/src/core/theme/app.theme.dart'; import 'package:counter_workshop/src/features/counter/data/repositories/counter.repository.dart'; import 'package:counter_workshop/src/features/counter/presentation/dashboard/bloc/dashboard.bloc.dart'; import 'package:counter_workshop/src/features/counter/presentation/dashboard/bloc/dashboard.event.dart'; -import 'package:counter_workshop/src/features/counter/presentation/dashboard/view/dashboard.page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -45,12 +45,14 @@ class AppView extends StatelessWidget { @override Widget build(BuildContext context) { final appTheme = AppTheme(); - return MaterialApp( + return MaterialApp.router( title: 'Counter Demo', theme: appTheme.light, darkTheme: appTheme.dark, themeMode: ThemeMode.system, - home: const DashboardPage(), + routeInformationProvider: router.routeInformationProvider, + routeInformationParser: router.routeInformationParser, + routerDelegate: router.routerDelegate, ); } } diff --git a/lib/src/core/routing/router.dart b/lib/src/core/routing/router.dart new file mode 100644 index 0000000..a46adfb --- /dev/null +++ b/lib/src/core/routing/router.dart @@ -0,0 +1,32 @@ +import 'package:counter_workshop/src/features/counter/presentation/dashboard/view/dashboard.page.dart'; +import 'package:counter_workshop/src/features/counter/presentation/edit/view/edit_counter.page.dart'; +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; + +final router = GoRouter( + urlPathStrategy: UrlPathStrategy.path, + debugLogDiagnostics: true, + initialLocation: '/counters', + routes: [ + GoRoute( + path: '/counters', + builder: (context, state) => const DashboardPage(), + routes: [ + GoRoute( + path: 'new', + pageBuilder: (context, state) => const MaterialPage( + fullscreenDialog: true, + child: EditCounterPage(), + ), + ), + GoRoute( + path: ':id', + builder: (context, state) { + final counterId = state.params['id']; + return EditCounterPage(counterId: counterId!); + }, + ), + ], + ), + ], +); diff --git a/lib/src/features/counter/presentation/dashboard/view/dashboard.page.dart b/lib/src/features/counter/presentation/dashboard/view/dashboard.page.dart index bda3324..0e3d57a 100644 --- a/lib/src/features/counter/presentation/dashboard/view/dashboard.page.dart +++ b/lib/src/features/counter/presentation/dashboard/view/dashboard.page.dart @@ -2,9 +2,9 @@ import 'package:counter_workshop/src/core/widgets/error_message.widget.dart'; import 'package:counter_workshop/src/features/counter/presentation/dashboard/bloc/dashboard.bloc.dart'; import 'package:counter_workshop/src/features/counter/presentation/dashboard/bloc/dashboard.state.dart'; import 'package:counter_workshop/src/features/counter/presentation/dashboard/view/widgets/counter_grid.dart'; -import 'package:counter_workshop/src/features/counter/presentation/edit/view/edit_counter.page.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:go_router/go_router.dart'; /// bloc class DashboardPage extends StatelessWidget { @@ -38,7 +38,7 @@ class DashboardPage extends StatelessWidget { floatingActionButton: FloatingActionButton( child: const Icon(Icons.add), onPressed: () { - Navigator.push(context, EditCounterPage.route(fullscreen: true)); + context.go('/counters/new'); }, ), ); diff --git a/lib/src/features/counter/presentation/dashboard/view/widgets/counter_grid.dart b/lib/src/features/counter/presentation/dashboard/view/widgets/counter_grid.dart index c2c0516..931699e 100644 --- a/lib/src/features/counter/presentation/dashboard/view/widgets/counter_grid.dart +++ b/lib/src/features/counter/presentation/dashboard/view/widgets/counter_grid.dart @@ -1,6 +1,6 @@ import 'package:counter_workshop/src/features/counter/domain/model/counter.model.dart'; -import 'package:counter_workshop/src/features/counter/presentation/edit/view/edit_counter.page.dart'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; class CounterGrid extends StatelessWidget { const CounterGrid({ @@ -26,7 +26,7 @@ class CounterGrid extends StatelessWidget { final counterModel = counterList[index]; return Card( child: InkWell( - onTap: () => Navigator.push(context, EditCounterPage.route(counterId: counterModel.id)), + onTap: () => context.push('/counters/${counterModel.id}'), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ diff --git a/lib/src/features/counter/presentation/edit/view/edit_counter.page.dart b/lib/src/features/counter/presentation/edit/view/edit_counter.page.dart index 2e6a793..361dadb 100644 --- a/lib/src/features/counter/presentation/edit/view/edit_counter.page.dart +++ b/lib/src/features/counter/presentation/edit/view/edit_counter.page.dart @@ -17,19 +17,6 @@ class EditCounterPage extends StatelessWidget { const EditCounterPage({this.counterId, super.key}); final String? counterId; - static Route route({String? counterId, bool fullscreen = false}) { - return MaterialPageRoute( - fullscreenDialog: fullscreen, - builder: (context) => BlocProvider( - create: (context) => EditCounterBloc( - counterRepository: context.read(), - counterId: counterId, - ), - child: EditCounterPage(counterId: counterId), - ), - ); - } - @override Widget build(BuildContext context) { final bloc = EditCounterBloc( diff --git a/pubspec.lock b/pubspec.lock index f418633..a72db10 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -214,6 +214,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" frontend_server_client: dependency: transitive description: @@ -228,6 +233,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + go_router: + dependency: "direct main" + description: + name: go_router + url: "https://pub.dartlang.org" + source: hosted + version: "4.4.1" graphs: dependency: transitive description: @@ -480,4 +492,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.18.0 <3.0.0" - flutter: ">=1.16.0" + flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 8f03ea3..43377bb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: equatable: ^2.0.5 http: ^0.13.5 flutter_bloc: ^8.1.1 + go_router: ^4.4.1 dev_dependencies: flutter_test: From 64a774a2f826bc471883b2ad383d7e52b04c69c3 Mon Sep 17 00:00:00 2001 From: Jan Marsh Date: Wed, 5 Oct 2022 22:45:26 +0200 Subject: [PATCH 2/3] fixing deep_linking on android --- Makefile | 2 +- android/app/src/main/AndroidManifest.xml | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 092fe22..bbbcd10 100644 --- a/Makefile +++ b/Makefile @@ -96,6 +96,6 @@ l10n: appicon: flutter pub run flutter_launcher_icons:main -f flutter_launcher_icons.yaml deeplink: - @printf "Android:\nadb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d 'https://counter.de/counters/2'" + @printf "Android:\nadb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d 'counter:///counters/2'" @printf "\n\n" @printf "iOS:\nxcrun simctl openurl booted counter:///counters/2" diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 4dbe308..ad69081 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -7,16 +7,17 @@ while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> + + + + - - - - +