migrate provider_shopper to go_router (#1540)

pull/1543/head
Miguel Beltran 2 years ago committed by GitHub
parent 616aa8d6be
commit 019441374e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
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:provider_shopper/common/theme.dart'; import 'package:provider_shopper/common/theme.dart';
import 'package:provider_shopper/models/cart.dart'; import 'package:provider_shopper/models/cart.dart';
@ -39,6 +40,28 @@ void setupWindow() {
} }
} }
GoRouter router() {
return GoRouter(
initialLocation: '/login',
routes: [
GoRoute(
path: '/login',
builder: (context, state) => const MyLogin(),
),
GoRoute(
path: '/catalog',
builder: (context, state) => const MyCatalog(),
routes: [
GoRoute(
path: 'cart',
builder: (context, state) => const MyCart(),
),
],
),
],
);
}
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
@ -62,15 +85,10 @@ class MyApp extends StatelessWidget {
}, },
), ),
], ],
child: MaterialApp( child: MaterialApp.router(
title: 'Provider Demo', title: 'Provider Demo',
theme: appTheme, theme: appTheme,
initialRoute: '/', routerConfig: router(),
routes: {
'/': (context) => const MyLogin(),
'/catalog': (context) => const MyCatalog(),
'/cart': (context) => const MyCart(),
},
), ),
); );
} }

@ -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:provider_shopper/models/cart.dart'; import 'package:provider_shopper/models/cart.dart';
import 'package:provider_shopper/models/catalog.dart'; import 'package:provider_shopper/models/catalog.dart';
@ -80,7 +81,7 @@ class _MyAppBar extends StatelessWidget {
actions: [ actions: [
IconButton( IconButton(
icon: const Icon(Icons.shopping_cart), icon: const Icon(Icons.shopping_cart),
onPressed: () => Navigator.pushNamed(context, '/cart'), onPressed: () => context.go('/catalog/cart'),
), ),
], ],
); );

@ -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';
class MyLogin extends StatelessWidget { class MyLogin extends StatelessWidget {
const MyLogin({super.key}); const MyLogin({super.key});
@ -36,7 +37,7 @@ class MyLogin extends StatelessWidget {
), ),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
Navigator.pushReplacementNamed(context, '/catalog'); context.pushReplacement('/catalog');
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
foregroundColor: Colors.yellow, foregroundColor: Colors.yellow,

@ -10,6 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
go_router: ^6.0.0
provider: ^6.0.2 provider: ^6.0.2
window_size: window_size:
git: git:

@ -5,10 +5,9 @@
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:provider_shopper/main.dart';
import 'package:provider_shopper/models/cart.dart'; import 'package:provider_shopper/models/cart.dart';
import 'package:provider_shopper/models/catalog.dart'; import 'package:provider_shopper/models/catalog.dart';
import 'package:provider_shopper/screens/catalog.dart';
import 'package:provider_shopper/screens/login.dart';
void main() { void main() {
testWidgets('Login page Widget test', (tester) async { testWidgets('Login page Widget test', (tester) async {
@ -23,13 +22,7 @@ void main() {
}, },
), ),
], ],
child: MaterialApp( child: MaterialApp.router(routerConfig: router()),
initialRoute: '/',
routes: {
'/': (context) => const MyLogin(),
'/catalog': (context) => const MyCatalog(),
},
),
)); ));
// Verifying the behaviour of ENTER button. // Verifying the behaviour of ENTER button.

Loading…
Cancel
Save