From 0f6a83258c8951c68380baa98980a9b116a8f9d6 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 13 Dec 2022 00:07:37 +0100 Subject: [PATCH] Add go_router to form_app (#1522) * Add go_router to form_app * format * add trailing comas --- form_app/lib/main.dart | 33 +++++++++++++++++++++++++-------- form_app/pubspec.yaml | 1 + 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/form_app/lib/main.dart b/form_app/lib/main.dart index f2b5f3bcf..423b894b5 100644 --- a/form_app/lib/main.dart +++ b/form_app/lib/main.dart @@ -6,6 +6,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; import 'package:window_size/window_size.dart'; import 'src/autofill.dart'; @@ -41,7 +42,7 @@ void setupWindow() { final demos = [ Demo( name: 'Sign in with HTTP', - route: '/signin_http', + route: 'signin_http', builder: (context) => SignInHttpDemo( // This sample uses a mock HTTP client. httpClient: mockClient, @@ -49,37 +50,53 @@ final demos = [ ), Demo( name: 'Autofill', - route: '/autofill', + route: 'autofill', builder: (context) => const AutofillDemo(), ), Demo( name: 'Form widgets', - route: '/form_widgets', + route: 'form_widgets', builder: (context) => const FormWidgetsDemo(), ), Demo( name: 'Validation', - route: '/validation', + route: 'validation', builder: (context) => const FormValidationDemo(), ), ]; +final router = GoRouter( + routes: [ + GoRoute( + path: '/', + builder: (context, state) => const HomePage(), + routes: [ + for (final demo in demos) + GoRoute( + path: demo.route, + builder: (context, state) => demo.builder(context), + ), + ], + ), + ], +); + class FormApp extends StatelessWidget { const FormApp({super.key}); @override Widget build(BuildContext context) { - return MaterialApp( + return MaterialApp.router( title: 'Form Samples', theme: ThemeData(primarySwatch: Colors.teal), - routes: Map.fromEntries(demos.map((d) => MapEntry(d.route, d.builder))), - home: const HomePage(), + routerConfig: router, ); } } class HomePage extends StatelessWidget { const HomePage({super.key}); + @override Widget build(BuildContext context) { return Scaffold( @@ -103,7 +120,7 @@ class DemoTile extends StatelessWidget { return ListTile( title: Text(demo!.name), onTap: () { - Navigator.pushNamed(context, demo!.route); + context.go('/${demo!.route}'); }, ); } diff --git a/form_app/pubspec.yaml b/form_app/pubspec.yaml index 9041639c1..ccd07cd85 100644 --- a/form_app/pubspec.yaml +++ b/form_app/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: git: url: https://github.com/google/flutter-desktop-embedding.git path: plugins/window_size + go_router: ^5.2.4 dev_dependencies: flutter_test: