【问题标题】:Flutter Navigator not working with named routesFlutter Navigator 不使用命名路由
【发布时间】:2020-11-07 13:30:44
【问题描述】:

我正在尝试使用导航器在命名路线之间导航。 尽管存在类似性质的问题here,但我没有发现答案特别有用。此外,当我尝试实施一些建议的解决方案时,它们都没有奏效。

相关错误是:

Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a
descendant of a Navigator widget.

我尝试删除和添加提供程序,但这似乎不是问题的原因,我已经测试了对Provider.of<T>(context) 接口的访问按预期工作。

我也尝试实现Builder 小部件,但这也无效。

为了澄清,Naigator.of(context).pushNamed('/home') 出现在 LandingPagebuild 方法中,用于调试目的。

代码:


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [Provider<LiraAnchor>(create: (_) => LiraAnchor())],
      child: MaterialApp(
        theme: ThemeData(
          primaryColor: LiraColours.highlightGreen,
          highlightColor: LiraColours.highlightGreen,
          accentColor: LiraColours.highlightGreen,
          cursorColor: LiraColours.highlightGreen,
          indicatorColor: LiraColours.highlightGreen,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        debugShowCheckedModeBanner: false,
        routes: {'/home': (ctx) => HomePage(), '/topup': (ctx) => TopUpPage()},
        builder: (ctx, _) {
          return LandingPage();
        },
      ),
    );
  }
}

class LandingPage extends StatefulWidget {
  LandingPage({Key key}) : super(key: key);

  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  final PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final double width = mediaQuery.size.width;
    final double height = mediaQuery.size.height;
    final List<Widget> onboarding = [
      SplashPage(width: width, height: height, pageController: _pageController),
      SignInPage(height: height, width: width)
    ];
    Navigator.of(context).pushNamed('/home');
    return Scaffold(
      backgroundColor: Color(0xFFECF0F3),
      body: LayoutBuilder(
          builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: viewportConstraints.maxHeight,
            ),
            child: IntrinsicHeight(
              child: SingleChildScrollView(
                child: Container(
                  width: width,
                  height: height,
                  child: PageView.builder(
                      controller: this._pageController,
                      itemCount: onboarding.length,
                      itemBuilder: (ctx, index) {
                        return onboarding[index];
                      }),
                ),
              ),
            ));
      }),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    替换这一行

    routes: {'/home': (ctx) => HomePage(), '/topup': (ctx) => TopUpPage()},
    

    initialRoute: RoutesLinks.main,
    onGenerateRoute: RoutesProvider.provideRoutes,
    

    并添加此文件

    class RoutesProvider {
    static Route<dynamic> provideRoutes(RouteSettings settings) {
    // Getting arguments passed, in while calling Navigator.pushNamed
    final arguments = settings.arguments;
    
    switch (settings.name) {
      case RoutesLinks.main:
        return MaterialPageRoute(builder: (_) => MainPage());
      case RoutesLinks.home:
        return MaterialPageRoute(builder: (_) => HomeScreen());
      default:
        // If there is no such named route in the switch statement, e.g. /third
        return _errorRoute();
    }
    }
    
    static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(builder: (_) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Error'),
        ),
        body: Center(
          child: Text('ERROR'),
        ),
      );
      });
     }
     }
    
     class RoutesLinks {
      static const main = '/';
      static const home = '/home';
     }
    

    【讨论】:

    • 嘿,刚刚试过,不幸的是似乎不起作用
    猜你喜欢
    • 2021-04-09
    • 2021-08-22
    • 2021-03-16
    • 2021-05-28
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    相关资源
    最近更新 更多