How solve problem Appbar icon clicked to navigate

2019-08-22 02:03发布

问题:

I try to develop flutter application. In this application I used Appbar with user icon to navigate another page.

Now I am clicked that icon(person icon) it shows error.

.

It has not proper documentation though internet. I couldn't found answer. My source code is

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());
                  }
                })));
  }

this is navigated class

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"),
      ),

    );
  }

}

Still I can't fix that error I am followed some documentation and stack overflow questions.

flutter documenttation

Github question and answer

Stackoverflow question and answer

回答1:

Try to use context in your builder

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


回答2:

The issue here is with Navigator not present in the parent context. You are using a context for the MyApp which isn't under the navigator.

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

to solve this - Define new class that contain MaterialApp then pass MyApp() in home: of MaterialApp.

Same for the 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());
              }
            }));
  }
}