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,

Share Improve this question edited Jan 30 at 13:13 Anna Andreeva Rogotulka 1,6449 gold badges18 silver badges23 bronze badges asked Jan 30 at 9:40 Amna ShaikhAmna Shaikh 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When 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'),
      ),
    );
  }
}

本文标签: flutterIn GetMaterialrouter GetoffAll() is not working how can I remove all previous routesStack Overflow