Anonymous
Обратная проблема Flutter Nested Navigator в Интернете
Сообщение
Anonymous » 13 янв 2025, 08:07
В приложении есть вложенный навигатор, как показано ниже
Код: Выделить всё
import 'package:aa/routes.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.teal,
),
initialRoute: root,
onGenerateRoute: AppRouter.mainRouteSettings,
navigatorKey: RouteConfig().appRouteKey,
);
}
}
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
RouteConfig().main.currentState!.maybePop();
return false;
},
child: Scaffold(
body: Container(
margin: const EdgeInsets.all(100),
decoration: BoxDecoration(
color: Colors.grey[500], borderRadius: BorderRadius.circular(20)),
child: Navigator(
key: RouteConfig().main,
initialRoute: one,
onGenerateRoute: AppRouter.generateRoute,
),
),
),
);
}
}
import 'package:aa/main.dart';
import 'package:flutter/material.dart';
//Pre
const String root = '/';
const String preRoute = '/preRoute';
const String one = '/';
const String two = '/two';
const String three = '/three';
class RouteConfig {
static final RouteConfig _routeConfig = RouteConfig._internal();
factory RouteConfig() {
return _routeConfig;
}
RouteConfig._internal();
///App Navigator Key
GlobalKey appRouteKey = GlobalKey();
///Pre Auth Key
GlobalKey main = GlobalKey();
}
class AppRouter {
static Route mainRouteSettings(RouteSettings settings) {
late Widget page;
switch (settings.name) {
case root:
page = Scaffold(
body: Container(
child: TextButton(
onPressed: () => RouteConfig()
.appRouteKey
.currentState!
.pushReplacementNamed(preRoute),
child: Center(child: Text('Click Me')))),
);
break;
case preRoute:
page = Test();
break;
default:
page = const Center(child: Text('Not Found'));
}
return MaterialPageRoute(
builder: (context) {
return page;
},
settings: settings,
);
}
static Route generateRoute(RouteSettings settings) {
late Widget page;
print(settings.name);
switch (settings.name) {
case one:
page = Builder(builder: (context) {
return WillPopScope(
onWillPop: () async => !Navigator.of(context).userGestureInProgress,
child: Container(
color: Colors.pink,
margin: const EdgeInsets.all(3),
child: Center(
child: Column(
children: [
TextButton(
onPressed: () {
RouteConfig().main.currentState!.pop();
},
child: Text('pop'),
),
TextButton(
onPressed: () {
RouteConfig().main.currentState!.pushNamed(two);
},
child: Text('dcdf'),
),
],
),
),
),
);
});
break;
case two:
page = const Text('Two');
break;
case three:
page = const Text('Three');
break;
default:
page = const Center(child: Text('Not Found'));
}
return MaterialPageRoute(
builder: (context) {
return page;
},
settings: settings,
);
}
}
Я могу провести назад по вложенному навигатору.
например, после того, как я нажму кнопку всплывающего окна, появится начальный маршрут вложенного навигатора, как может идти вложенный навигатор назад, когда это начальный маршрут приложения, как предотвратить такое поведение.
См., например, видео
Подробнее здесь:
https://stackoverflow.com/questions/711 ... sue-on-web
1736744856
Anonymous
В приложении есть вложенный навигатор, как показано ниже [code]import 'package:aa/routes.dart'; import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.teal, ), initialRoute: root, onGenerateRoute: AppRouter.mainRouteSettings, navigatorKey: RouteConfig().appRouteKey, ); } } class Test extends StatelessWidget { const Test({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async { RouteConfig().main.currentState!.maybePop(); return false; }, child: Scaffold( body: Container( margin: const EdgeInsets.all(100), decoration: BoxDecoration( color: Colors.grey[500], borderRadius: BorderRadius.circular(20)), child: Navigator( key: RouteConfig().main, initialRoute: one, onGenerateRoute: AppRouter.generateRoute, ), ), ), ); } } import 'package:aa/main.dart'; import 'package:flutter/material.dart'; //Pre const String root = '/'; const String preRoute = '/preRoute'; const String one = '/'; const String two = '/two'; const String three = '/three'; class RouteConfig { static final RouteConfig _routeConfig = RouteConfig._internal(); factory RouteConfig() { return _routeConfig; } RouteConfig._internal(); ///App Navigator Key GlobalKey appRouteKey = GlobalKey(); ///Pre Auth Key GlobalKey main = GlobalKey(); } class AppRouter { static Route mainRouteSettings(RouteSettings settings) { late Widget page; switch (settings.name) { case root: page = Scaffold( body: Container( child: TextButton( onPressed: () => RouteConfig() .appRouteKey .currentState! .pushReplacementNamed(preRoute), child: Center(child: Text('Click Me')))), ); break; case preRoute: page = Test(); break; default: page = const Center(child: Text('Not Found')); } return MaterialPageRoute( builder: (context) { return page; }, settings: settings, ); } static Route generateRoute(RouteSettings settings) { late Widget page; print(settings.name); switch (settings.name) { case one: page = Builder(builder: (context) { return WillPopScope( onWillPop: () async => !Navigator.of(context).userGestureInProgress, child: Container( color: Colors.pink, margin: const EdgeInsets.all(3), child: Center( child: Column( children: [ TextButton( onPressed: () { RouteConfig().main.currentState!.pop(); }, child: Text('pop'), ), TextButton( onPressed: () { RouteConfig().main.currentState!.pushNamed(two); }, child: Text('dcdf'), ), ], ), ), ), ); }); break; case two: page = const Text('Two'); break; case three: page = const Text('Three'); break; default: page = const Center(child: Text('Not Found')); } return MaterialPageRoute( builder: (context) { return page; }, settings: settings, ); } } [/code] Я могу провести назад по вложенному навигатору. например, после того, как я нажму кнопку всплывающего окна, появится начальный маршрут вложенного навигатора, как может идти вложенный навигатор назад, когда это начальный маршрут приложения, как предотвратить такое поведение. См., например, видео Подробнее здесь: [url]https://stackoverflow.com/questions/71186987/flutter-nested-navigator-back-issue-on-web[/url]