admin管理员组文章数量:1315354
class MyApp extends StatefulWidget {
MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return GetMaterialApp.router(
routerDelegate: AppRouterDelegate(),
navigatorObservers: [RouteObserverService()],
getPages: Routes.list,
popGesture: true,
enableLog: true,
debugShowCheckedModeBanner: false,
title: '',
initialBinding: InitBindings(),
theme: ThemeData(
fontFamily: AppConstant.fontFamily,
dividerColor: Colors.transparent,
appBarTheme: const AppBarTheme(
color: Color(0xFFFFFFFF),
),
),
);
}
}
class AppRouterDelegate extends GetDelegate {
@override
Widget build(BuildContext context) {
return Navigator(
key: Get.key,
onPopPage: (route, result) => route.didPop(result),
pages: currentConfiguration != null
? [currentConfiguration!.currentPage!]
: [GetNavConfig.fromRoute(Routes.landingPage)!.currentPage!],
);
}
}
this is my code. I really don't know How can I remove all previous routes, similar to using Get.offAllNamed(newRouteName)
, when working with GetMaterialApp.router
?
when I am using Get.offAllNamed()
it gives me error of You are trying to use contextless navigation without a GetMaterialApp
or Get.key
. If you are testing your app,
class MyApp extends StatefulWidget {
MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return GetMaterialApp.router(
routerDelegate: AppRouterDelegate(),
navigatorObservers: [RouteObserverService()],
getPages: Routes.list,
popGesture: true,
enableLog: true,
debugShowCheckedModeBanner: false,
title: '',
initialBinding: InitBindings(),
theme: ThemeData(
fontFamily: AppConstant.fontFamily,
dividerColor: Colors.transparent,
appBarTheme: const AppBarTheme(
color: Color(0xFFFFFFFF),
),
),
);
}
}
class AppRouterDelegate extends GetDelegate {
@override
Widget build(BuildContext context) {
return Navigator(
key: Get.key,
onPopPage: (route, result) => route.didPop(result),
pages: currentConfiguration != null
? [currentConfiguration!.currentPage!]
: [GetNavConfig.fromRoute(Routes.landingPage)!.currentPage!],
);
}
}
this is my code. I really don't know How can I remove all previous routes, similar to using Get.offAllNamed(newRouteName)
, when working with GetMaterialApp.router
?
when I am using Get.offAllNamed()
it gives me error of You are trying to use contextless navigation without a GetMaterialApp
or Get.key
. If you are testing your app,
1 Answer
Reset to default 0When you use GetX's router for navigation, some normal navigation commands won't work.
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class Routes {
static const String landingPage = '/landing';
static const String newRoute = '/new_route';
static List<GetPage> list = [
GetPage(name: landingPage, page: () => LandingPage()),
GetPage(name: newRoute, page: () => NewRoutePage()),
];
}
class RouteObserverService extends RouteObserver<PageRoute<dynamic>> {}
class AppConstant {
static const String fontFamily = 'Roboto';
}
class InitBindings extends Bindings {
@override
void dependencies() {
//=> Add your dependencies here
}
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return GetMaterialApp.router(
routerDelegate: AppRouterDelegate(),
navigatorObservers: [RouteObserverService()],
getPages: Routes.list,
popGesture: true,
enableLog: true,
debugShowCheckedModeBanner: false,
title: '',
initialBinding: InitBindings(),
theme: ThemeData(
fontFamily: AppConstant.fontFamily,
dividerColor: Colors.transparent,
appBarTheme: const AppBarTheme(
color: Color(0xFFFFFFFF),
),
),
);
}
}
class AppRouterDelegate extends GetDelegate {
//=> Function to handle Get.offAllNamed
void offAllNamed(String routeName) {
currentConfiguration = GetNavConfig.fromRoute(routeName);
notifyListeners(); //=> Here Navigator to rebuild
}
@override
Widget build(BuildContext context) {
return Navigator(
key: Get.key,
onPopPage: (route, result) => route.didPop(result),
pages: currentConfiguration != null
? [currentConfiguration!.currentPage!]
: [GetNavConfig.fromRoute(Routes.landingPage)!.currentPage!],
);
}
}
// Example Usage:
class MyController extends GetxController {
final AppRouterDelegate routerDelegate = Get.find<AppRouterDelegate>(); //=> Get the delegate instance
void navigateToNewRoute() {
routerDelegate.offAllNamed(Routes.newRoute); //=> Use the custom function
}
@override
void onInit() {
Get.put(AppRouterDelegate()); //=> Register the delegate
super.onInit();
}
}
class LandingPage extends StatelessWidget {
const LandingPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Landing Page')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('This is the Landing Page'),
ElevatedButton(
onPressed: () {
Get.find<MyController>().navigateToNewRoute(); //=> Access the navigation function
},
child: const Text('Go to New Route'),
),
],
),
),
);
}
}
class NewRoutePage extends StatelessWidget {
const NewRoutePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('New Route Page')),
body: const Center(
child: Text('This is the New Route Page'),
),
);
}
}
版权声明:本文标题:flutter - In GetMaterial.router Get.offAll() is not working. how can I remove all previous routes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975325a2408086.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论