From 329c531dfca7670f7f5a0b11dcd14c77895fc45f Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 20 Dec 2022 10:03:26 +0100 Subject: [PATCH] Migrate platform_channels to go_router (#1533) * Migrate platform_channels to go_router * code format * move addPetDetails into a subroute of petListScreen * code format * refactor router and fix tests * removed unused import * Elide `web_dashboard` from Master CI (#1535) * Bump ossf/scorecard-action from 2.1.0 to 2.1.1 (#1536) Bumps [ossf/scorecard-action](https://github.com/ossf/scorecard-action) from 2.1.0 to 2.1.1. - [Release notes](https://github.com/ossf/scorecard-action/releases) - [Changelog](https://github.com/ossf/scorecard-action/blob/main/RELEASE.md) - [Commits](https://github.com/ossf/scorecard-action/compare/937ffa90d79c7d720498178154ad4c7ba1e4ad8c...15c10fcf1cf912bd22260bfec67569a359ab87da) --- updated-dependencies: - dependency-name: ossf/scorecard-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * go_router 6.0.0 Signed-off-by: dependabot[bot] Co-authored-by: Brett Morgan Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- platform_channels/lib/main.dart | 50 +++++++++++++++---- .../lib/src/add_pet_details.dart | 3 +- .../lib/src/pet_list_screen.dart | 3 +- platform_channels/pubspec.yaml | 1 + .../test/src/add_pet_details_test.dart | 12 ++++- 5 files changed, 55 insertions(+), 14 deletions(-) diff --git a/platform_channels/lib/main.dart b/platform_channels/lib/main.dart index d12250f4e..d80d7cc1a 100644 --- a/platform_channels/lib/main.dart +++ b/platform_channels/lib/main.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; import 'package:platform_channels/src/add_pet_details.dart'; import 'package:platform_channels/src/event_channel_demo.dart'; import 'package:platform_channels/src/method_channel_demo.dart'; @@ -18,25 +19,54 @@ class PlatformChannelSample extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( - routes: { - '/methodChannelDemo': (context) => const MethodChannelDemo(), - '/eventChannelDemo': (context) => const EventChannelDemo(), - '/platformImageDemo': (context) => const PlatformImageDemo(), - '/petListScreen': (context) => const PetListScreen(), - '/addPetDetails': (context) => const AddPetDetails(), - }, + return MaterialApp.router( title: 'Platform Channel Sample', theme: ThemeData( snackBarTheme: SnackBarThemeData( backgroundColor: Colors.blue[500], ), ), - home: const HomePage(), + routerConfig: router(), ); } } +GoRouter router([String? initialLocation]) { + return GoRouter( + initialLocation: initialLocation ?? '/', + routes: [ + GoRoute( + path: '/', + builder: (context, state) => const HomePage(), + routes: [ + GoRoute( + path: 'methodChannelDemo', + builder: (context, state) => const MethodChannelDemo(), + ), + GoRoute( + path: 'eventChannelDemo', + builder: (context, state) => const EventChannelDemo(), + ), + GoRoute( + path: 'platformImageDemo', + builder: (context, state) => const PlatformImageDemo(), + ), + GoRoute( + path: 'petListScreen', + builder: (context, state) => const PetListScreen(), + routes: [ + GoRoute( + path: 'addPetDetails', + builder: (context, state) => const AddPetDetails(), + ), + ], + ), + ], + ), + ], + ); +} + class DemoInfo { final String demoTitle; final String demoRoute; @@ -90,7 +120,7 @@ class DemoTile extends StatelessWidget { return ListTile( title: Text(demoInfo.demoTitle), onTap: () { - Navigator.pushNamed(context, demoInfo.demoRoute); + context.go(demoInfo.demoRoute); }, ); } diff --git a/platform_channels/lib/src/add_pet_details.dart b/platform_channels/lib/src/add_pet_details.dart index 96151b370..bc87007ce 100644 --- a/platform_channels/lib/src/add_pet_details.dart +++ b/platform_channels/lib/src/add_pet_details.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; import 'package:platform_channels/src/pet_list_message_channel.dart'; /// Demonstrates how to use [BasicMessageChannel] to send a message to platform. @@ -36,7 +37,7 @@ class _AddPetDetailsState extends State { ), ); - Navigator.pop(context); + context.pop(); }, ) ], diff --git a/platform_channels/lib/src/pet_list_screen.dart b/platform_channels/lib/src/pet_list_screen.dart index b36a2b850..3ea9f1389 100644 --- a/platform_channels/lib/src/pet_list_screen.dart +++ b/platform_channels/lib/src/pet_list_screen.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:go_router/go_router.dart'; import 'package:platform_channels/src/pet_list_message_channel.dart'; /// Demonstrates how to use [BasicMessageChannel] to send & receive the platform @@ -52,7 +53,7 @@ class _PetListScreenState extends State { floatingActionButton: FloatingActionButton( child: const Icon(Icons.add), onPressed: () { - Navigator.pushNamed(context, '/addPetDetails'); + context.go('/petListScreen/addPetDetails'); }, ), body: petListModel.petList.isEmpty diff --git a/platform_channels/pubspec.yaml b/platform_channels/pubspec.yaml index 87dba9a66..24bb54c5e 100644 --- a/platform_channels/pubspec.yaml +++ b/platform_channels/pubspec.yaml @@ -11,6 +11,7 @@ dependencies: sdk: flutter cupertino_icons: ^1.0.3 + go_router: ^6.0.0 dev_dependencies: flutter_test: diff --git a/platform_channels/test/src/add_pet_details_test.dart b/platform_channels/test/src/add_pet_details_test.dart index 0689fc3a1..cd8bbec1b 100644 --- a/platform_channels/test/src/add_pet_details_test.dart +++ b/platform_channels/test/src/add_pet_details_test.dart @@ -5,7 +5,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:platform_channels/src/add_pet_details.dart'; +import 'package:platform_channels/main.dart' as app; void main() { group('AddPetDetails tests', () { @@ -20,7 +20,12 @@ void main() { }); testWidgets('Enter pet details', (tester) async { - await tester.pumpWidget(const MaterialApp(home: AddPetDetails())); + var router = app.router('/petListScreen/addPetDetails'); + await tester.pumpWidget( + MaterialApp.router( + routerConfig: router, + ), + ); // Enter the breed of cat. await tester.enterText(find.byType(TextField), 'Persian'); @@ -33,6 +38,9 @@ void main() { expect(petList, isNotEmpty); expect(petList.last['breed'], 'Persian'); + + // Navigate back to /petListScreen + expect(router.location, '/petListScreen/'); }); }); }