【问题标题】:How solve problem Appbar icon clicked to navigate如何解决Appbar图标点击导航问题
【发布时间】:2019-07-18 04:27:57
【问题描述】:

我尝试开发颤振应用程序。在这个应用程序中,我使用带有用户图标的 Appbar 来导航另一个页面。

现在我点击了显示错误的图标(人形图标)。

互联网上没有适当的文档。我找不到答案。我的源代码是

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text("Web Issue finder"),
              actions: <Widget>[
                new IconButton(
                  icon: Icon(Icons.person),
//                  tooltip: "Admin",
                  onPressed: (){
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (_) => AdminAuth()),
                    );
                  }
                )
              ],
            ),
            body: new FutureBuilder(
                future: loadStates(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return new ListView.builder(
                      itemBuilder: (context, index) {
                        if (index >= snapshot?.data?.length ?? 0) return null;

                        return new ListTile(
                          title: new Text("${snapshot.data[index]}"),
                          onTap: () {
                            debugPrint("${snapshot.data[index]} clicked");
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) =>
                                    IssueAddScreen(state: snapshot.data[index]),
                              ),
                            );
                          },
                        );
                      },
                    );
                  } else {
                    return new Center(child: new CircularProgressIndicator());
                  }
                })));
  }

这是导航类

import 'package:flutter/material.dart';


class AdminAuth extends StatelessWidget{

//  final String state;

//  IssueAddScreen({Key key, @required this.state}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "iWallet",
      home: Scaffold(
        appBar: AppBar(title: Text("admin auth"),),
        body: Text("cvgbh"),
      ),

    );
  }

}

我仍然无法修复该错误,我遵循了一些文档和堆栈溢出问题。

flutter documenttation

Github question and answer

Stackoverflow question and answer

【问题讨论】:

    标签: flutter navigator appbar flutter-navigation flutter-appbar


    【解决方案1】:

    尝试在构建器中使用上下文

    Navigator.push(context,MaterialPageRoute(builder: (BuildContext context){return AdminAuth();
    });
    

    【讨论】:

    • 不,它不适合我。错误和以前一样
    【解决方案2】:

    这里的问题是导航器不存在于父上下文中。 您正在使用不在导航器下的 MyApp 的上下文。

    MyApp    <------ context
      --> MaterialApp
       (--> Navigator built within MaterialApp)
          --> Scaffold
            --> App Bar
              --> ...
    

    解决这个问题 - 定义包含 MaterialApp 的新类,然后在 home:MaterialApp 中传递 MyApp()

    AdminAuth 也是如此。

    class MyAppHome extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyApp(),
        );
      }
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: new AppBar(
              title: new Text("Web Issue finder"),
              actions: <Widget>[
                new IconButton(
                    icon: Icon(Icons.person),
    //                  tooltip: "Admin",
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (_) => AdminAuth()),
                      );
                    })
              ],
            ),
            body: new FutureBuilder(
                future: loadStates(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return new ListView.builder(
                      itemBuilder: (context, index) {
                        if (index >= snapshot?.data?.length ?? 0) return null;
    
                        return new ListTile(
                          title: new Text("${snapshot.data[index]}"),
                          onTap: () {
                            debugPrint("${snapshot.data[index]} clicked");
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) =>
                                    IssueAddScreen(state: snapshot.data[index]),
                              ),
                            );
                          },
                        );
                      },
                    );
                  } else {
                    return new Center(child: new CircularProgressIndicator());
                  }
                }));
      }
    }
    

    【讨论】:

      【解决方案3】:

      问题是上面解释的问题。 用我自己的话: 您从中调用“导航器”的上下文不包含“导航器”。 我想问题是在您的代码中,您在 MaterialApp 完成构建方法并获得 Navigator 或类似的东西之前调用了 Scaffold。 如果您将 MaterialApp 和 Scaffold 分开(如下所示),您就可以解决问题。

      void main() => runApp(MaterialApp(
          home: MyApp())
      
      
      class MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
      return new Scaffold(
              appBar: new AppBar(
                title: new Text("Web Issue finder"),
                actions: <Widget>[
                  new IconButton(
                    icon: Icon(Icons.person),
                  tooltip: "Admin",
                    onPressed: (){
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (_) => AdminAuth()),
                      );
                    }
                  )
                ],
              ),
              body: new FutureBuilder(
                  future: loadStates(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return new ListView.builder(
                        itemBuilder: (context, index) {
                          if (index >= snapshot?.data?.length ?? 0) return null;
      
                          return new ListTile(
                            title: new Text("${snapshot.data[index]}"),
                            onTap: () {
                              debugPrint("${snapshot.data[index]} clicked");
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) =>
                                      IssueAddScreen(state: snapshot.data[index]),
                                ),
                              );
                            },
                          );
                        },
                      );
                    } else {
                      return new Center(child: new CircularProgressIndicator());
                    }
                  })));
      

      【讨论】:

        【解决方案4】:
        IconButton(onPressed: () {
                   Navigator.of(context).push(new MaterialPageRoute(builder: (context)=> AdminAuth));
                },)
        

        【讨论】:

        • 不要只包含代码,稍微解释一下代码在做什么。
        【解决方案5】:

        库中的 MateriaApp 上下文存在一些问题。 您的代码将不起作用。创建一个不同的 MaterialApp,然后在 MaterialApp 的 home: 属性中使用您的小部件。

        例如:

        void main() => runApp(MyApp());
        
        class MyApp extends StatelessWidget {
          // This widget is the root of your application.
          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              title: 'Flutter Demo',
              debugShowCheckedModeBanner: false,
              theme: ThemeData(
                primarySwatch: Colors.lightBlue,
              ),
              home: MyAppState(), //This is your MyAppState
            );
          }
        }
        

        现在您可以删除 MyAppState 中的 MaterialApp 小部件,仅保留 Scaffold 小部件

        【讨论】:

          猜你喜欢
          • 2021-06-07
          • 1970-01-01
          • 2020-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多