使用 GoRouter 进行 Flutter 导航:Go 与 Push

您所在的位置:网站首页 routerpush和routergo 使用 GoRouter 进行 Flutter 导航:Go 与 Push

使用 GoRouter 进行 Flutter 导航:Go 与 Push

#使用 GoRouter 进行 Flutter 导航:Go 与 Push| 来源: 网络整理| 查看: 265

import 'package:flutter/material.dart';import 'package:go_router/go_router.dart';

void main() { WidgetsFlutterBinding.ensureInitialized(); GoRouter.setUrlPathStrategy(UrlPathStrategy.path); runApp(const MyApp());}

class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { final goRouter = GoRouter( debugLogDiagnostics: true, initialLocation: '/', routes: [ GoRoute( path: '/', builder: (context, state) => const HomeScreen(), routes: [ GoRoute( path: 'detail', builder: (context, state) => const DetailScreen(), ), GoRoute( path: 'modal', pageBuilder: (context, state) => const MaterialPage( fullscreenDialog: true, child: ModalScreen(), ), ) ], ), ], ); return MaterialApp.router( routerDelegate: goRouter.routerDelegate, routeInformationParser: goRouter.routeInformationParser, debugShowCheckedModeBanner: false, theme: ThemeData( primarySwatch: Colors.amber, ), ); }}

class HomeScreen extends StatelessWidget { const HomeScreen({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Home Page'), backgroundColor: Colors.red, ), backgroundColor: Colors.red, body: Padding( padding: const EdgeInsets.all(32.0), child: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.min, children: [ ElevatedButton( onPressed: () => context.go('/detail'), child: const CenteredText('go /detail'), ), const SizedBox( height: 32, ), ElevatedButton( onPressed: () => context.push('/detail'), child: const CenteredText('push /detail'), ), const SizedBox( height: 32, ), ElevatedButton( onPressed: () => context.go('/modal'), child: const CenteredText('go /modal'), ), ], ), ), ), ); }}

class DetailScreen extends StatelessWidget { const DetailScreen({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Detail Page'), backgroundColor: Colors.green, ), backgroundColor: Colors.green, body: Padding( padding: const EdgeInsets.all(32.0), child: Center( child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, mainAxisSize: MainAxisSize.min, children: [ ElevatedButton( onPressed: () => context.go('/modal'), child: const CenteredText('go /modal'), ), const SizedBox( height: 32, ), ElevatedButton( onPressed: () => context.push('/modal'), child: const CenteredText('push /modal'), ), ], ), ), ), ); }}

class ModalScreen extends StatelessWidget { const ModalScreen({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Modal Page'), backgroundColor: Colors.blue, ), backgroundColor: Colors.blue, ); }}

class CenteredText extends StatelessWidget { const CenteredText(this.text, {Key? key}) : super(key: key); final String text; @override Widget build(BuildContext context) { return Text( text, style: const TextStyle(fontSize: 30), textAlign: TextAlign.center, ); }}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3