如何解决问题Appbar图标单击导航

时间:2019-02-24 17:59:42

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

我尝试开发Flutter应用程序。在此应用程序中,我使用带有用户图标的Appbar浏览另一个页面。

emulator with Appbar and person icon

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

error in debuger

它没有通过互联网提供的适当文档。我找不到答案。我的源代码是

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

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

要解决此问题-定义包含MaterialApp的新类,然后在MyApp()的{​​{1}}中传递home:

MaterialApp相同。

AdminAuth

答案 2 :(得分:0)

问题是上面说明的问题。 用我自己的话: 您从中调用“导航器”的上下文不包含“导航器”。 我猜问题是在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());
              }
            })));

答案 3 :(得分:0)

IconButton(onPressed: () {
           Navigator.of(context).push(new MaterialPageRoute(builder: (context)=> AdminAuth));
        },)

答案 4 :(得分:0)

库中的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 小部件