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/'); }); }); }