【问题标题】:Using onTap to navigate to a specific page使用 onTap 导航到特定页面
【发布时间】:2017-07-12 13:55:38
【问题描述】:

每当我尝试通过单击抽屉中的图标进行导航时都会收到此错误

当我点击抽屉的物品时出现错误信息

...onTap:_profile
//jump to function...
Widget build (BuildContext context){
void _profile() {
  Navigator.popAndPushNamed(context, "/Profile");}...

这是主函数的样子

    void main() {
  runApp(new MaterialApp(
    home: new LoginPage(),

  routes: <String, WidgetBuilder>{
    "/Feed": (BuildContext context) => new first.Feed(),
    "/Settings":(BuildContext context) => new sixth.Settings(),
    "/Profile": (BuildContext context) => new fifth.Profile(),
    "/Search": (BuildContext context) => new second.Search(),
    //"/Signout":(BuildContext context) => new LogoutPage(),
    "/Notify": (BuildContext context) => new third.Notifications(),
    "/Feedback": (BuildContext context) => new fourth.Feedback(),
    "/Tabs": (BuildContext context) => new Tabs(),.....

更新:

我正在使用带有 firebase 身份验证的 google sigin,但我需要在登录后重定向用户才能查看我的应用程序。

我是新来的,所以我仍然不知道如何解决这个问题,所以我做了什么

Future<String> _testSignInWithGoogle() async{
//implement signin
runApp(
  new MaterialApp(
    home: new Tabs(),
  )
);
 return 'signInWithGoogle succeeded: $user';
}

令人困惑,但我不明白在登录后如何重定向用户以查看 Tabs(),我的程序的当前流程是: loginPage()->onPressed: _signIn -> 调用 _testSignInWithGoogle()-> 创建新的 Tabs()。

【问题讨论】:

  • 您能否包含对 new MaterialApp 的调用,以便我们查看您的路线是如何设置的?
  • @CollinJackson 我已经在帖子中添加了主要功能。

标签: dart flutter


【解决方案1】:

它对我来说很好,所以如果可以的话,请发布一个简化的测试用例。您确定您的代码中没有任何其他 NavigatorMaterialApp 实例吗?

这是我用来测试的代码。

import 'package:flutter/material.dart';

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    void _profile() {
      Navigator.popAndPushNamed(context, "/Profile");
    }
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Login'),
      ),
      drawer: new Drawer(
        child: new InkWell(
          onTap: _profile,
          child: new Icon(Icons.navigate_next),
        ),
      )
    );
  }
}

class Profile extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Text('You made it!'),
      ),
    );
  }
}

void main() {
  runApp(new MaterialApp(
    home: new LoginPage(),

  routes: <String, WidgetBuilder>
  {
  "/Profile": (BuildContext context) => new Profile(),
  }
  ));
}

如果您无法弄清楚您的版本和我的版本有什么不同,您可以尝试实现onGenerateRoute,而不是将routes 参数传递给MaterialApp

【讨论】:

  • 我想你指出了问题,是的,除了主函数中包含的那个之外,我确实有另一个 MaterialApp() 实例。我已经相应地更新了帖子。
  • 哦,我发现我错过了在 _testSignInWithGoogle() 方法中的 MaterialApp() 中设置路线!非常感谢您的帮助。
  • 请问您是否认为我的结构在逻辑上是正确的? @科林杰克逊
  • 你所做的很好,我通常会尽量避免多次调用runApp,因为多个MaterialApps 会使程序更难推理。将popAndPushNamed 视为返回登录页面的一种方式。
  • 会试试这个,再次感谢您提供有用的反馈。
猜你喜欢
  • 2014-04-08
  • 2021-02-11
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 2020-12-25
  • 2018-04-22
  • 1970-01-01
相关资源
最近更新 更多