Migrate testing_app to go_router (#1541)

* add go_router

* migrate testing_app to go_router
pull/1543/head
Miguel Beltran 2 years ago committed by GitHub
parent 329c531dfc
commit 616aa8d6be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:testing_app/models/favorites.dart'; import 'package:testing_app/models/favorites.dart';
import 'package:testing_app/screens/favorites.dart'; import 'package:testing_app/screens/favorites.dart';
@ -12,6 +13,23 @@ void main() {
runApp(const TestingApp()); runApp(const TestingApp());
} }
GoRouter router() {
return GoRouter(
routes: [
GoRoute(
path: HomePage.routeName,
builder: (context, state) => const HomePage(),
routes: [
GoRoute(
path: FavoritesPage.routeName,
builder: (context, state) => const FavoritesPage(),
),
],
),
],
);
}
class TestingApp extends StatelessWidget { class TestingApp extends StatelessWidget {
const TestingApp({super.key}); const TestingApp({super.key});
@ -19,17 +37,13 @@ class TestingApp extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider<Favorites>( return ChangeNotifierProvider<Favorites>(
create: (context) => Favorites(), create: (context) => Favorites(),
child: MaterialApp( child: MaterialApp.router(
title: 'Testing Sample', title: 'Testing Sample',
theme: ThemeData( theme: ThemeData(
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity, visualDensity: VisualDensity.adaptivePlatformDensity,
), ),
routes: { routerConfig: router(),
HomePage.routeName: (context) => const HomePage(),
FavoritesPage.routeName: (context) => const FavoritesPage(),
},
initialRoute: HomePage.routeName,
), ),
); );
} }

@ -7,7 +7,8 @@ import 'package:provider/provider.dart';
import 'package:testing_app/models/favorites.dart'; import 'package:testing_app/models/favorites.dart';
class FavoritesPage extends StatelessWidget { class FavoritesPage extends StatelessWidget {
static const routeName = '/favorites_page'; static const routeName = 'favorites_page';
static const fullPath = '/$routeName';
const FavoritesPage({super.key}); const FavoritesPage({super.key});

@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:testing_app/models/favorites.dart'; import 'package:testing_app/models/favorites.dart';
import 'package:testing_app/screens/favorites.dart'; import 'package:testing_app/screens/favorites.dart';
@ -21,7 +22,7 @@ class HomePage extends StatelessWidget {
TextButton.icon( TextButton.icon(
style: TextButton.styleFrom(foregroundColor: Colors.white), style: TextButton.styleFrom(foregroundColor: Colors.white),
onPressed: () { onPressed: () {
Navigator.pushNamed(context, FavoritesPage.routeName); context.go(FavoritesPage.fullPath);
}, },
icon: const Icon(Icons.favorite_border), icon: const Icon(Icons.favorite_border),
label: const Text('Favorites'), label: const Text('Favorites'),

@ -12,6 +12,7 @@ dependencies:
cupertino_icons: ^1.0.3 cupertino_icons: ^1.0.3
provider: ^6.0.2 provider: ^6.0.2
go_router: ^6.0.0
dev_dependencies: dev_dependencies:
integration_test: integration_test:

@ -5,13 +5,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:testing_app/main.dart';
import 'package:testing_app/models/favorites.dart'; import 'package:testing_app/models/favorites.dart';
import 'package:testing_app/screens/home.dart';
Widget createHomeScreen() => ChangeNotifierProvider<Favorites>( Widget createHomeScreen() => ChangeNotifierProvider<Favorites>(
create: (context) => Favorites(), create: (context) => Favorites(),
child: const MaterialApp( child: MaterialApp.router(
home: HomePage(), routerConfig: router(),
), ),
); );
@ -65,5 +65,16 @@ void main() {
// Check if the filled icon changes back to bordered icon. // Check if the filled icon changes back to bordered icon.
expect(find.byIcon(Icons.favorite), findsNothing); expect(find.byIcon(Icons.favorite), findsNothing);
}); });
testWidgets('Testing Navigation', (tester) async {
await tester.pumpWidget(createHomeScreen());
// Tap the Favorites button in the app bar
await tester.tap(find.text('Favorites'));
await tester.pumpAndSettle();
// Verify if the empty favorites screen is shown.
expect(find.text('No favorites added.'), findsOneWidget);
});
}); });
} }

Loading…
Cancel
Save