mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Migrate testing_app to go_router (#1541)
* add go_router * migrate testing_app to go_router
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
import 'package:testing_app/screens/favorites.dart';
|
||||
@@ -12,6 +13,23 @@ void main() {
|
||||
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 {
|
||||
const TestingApp({super.key});
|
||||
|
||||
@@ -19,17 +37,13 @@ class TestingApp extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return ChangeNotifierProvider<Favorites>(
|
||||
create: (context) => Favorites(),
|
||||
child: MaterialApp(
|
||||
child: MaterialApp.router(
|
||||
title: 'Testing Sample',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
routes: {
|
||||
HomePage.routeName: (context) => const HomePage(),
|
||||
FavoritesPage.routeName: (context) => const FavoritesPage(),
|
||||
},
|
||||
initialRoute: HomePage.routeName,
|
||||
routerConfig: router(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
|
||||
class FavoritesPage extends StatelessWidget {
|
||||
static const routeName = '/favorites_page';
|
||||
static const routeName = 'favorites_page';
|
||||
static const fullPath = '/$routeName';
|
||||
|
||||
const FavoritesPage({super.key});
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
import 'package:testing_app/screens/favorites.dart';
|
||||
@@ -21,7 +22,7 @@ class HomePage extends StatelessWidget {
|
||||
TextButton.icon(
|
||||
style: TextButton.styleFrom(foregroundColor: Colors.white),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, FavoritesPage.routeName);
|
||||
context.go(FavoritesPage.fullPath);
|
||||
},
|
||||
icon: const Icon(Icons.favorite_border),
|
||||
label: const Text('Favorites'),
|
||||
|
||||
Reference in New Issue
Block a user