StatefulWidget:通过上下文请求的导航器操作

时间:2019-11-17 21:30:41

标签: flutter dart

我正在尝试导航到另一个屏幕。但是,我得到了:

  

引发了另一个异常:请求导航器操作并带有   不包含导航器的上下文

我通过@ burhanrashid52在GitHub(https://github.com/flutter/flutter/issues/15919)上找到了解决方案。但是,他使用的示例是使用StatelessWidget。有人可以给我一个如何使用StatefulWidget解决此问题的示例吗?谢谢。

class ToDoState extends State<ToDo> {
  @override
  Widget build (BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          backgroundColor: Color(0xff00bbff),
          child: Icon(
            Icons.chat_bubble
          ),
          onPressed: () {
            Navigator.push(context, MaterialPageRoute(builder: (context) => NewTask()));
            print("test");
          }
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SafeArea(
              child: Container(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(20, 40, 20, 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        "Task List",
                        style: TextStyle(
                          fontWeight: FontWeight.w800,
                          fontSize: 22,
                          color: Color(0xff242424)
                        ),
                      ),
                      Text(
                        "1 incomplete",
                        style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: 14,
                          color: Color(0xffb3b3b3)
                        ),
                      ),
                    ],
                  )
                ),
              ),
            ),
            Expanded(
              child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  Item("Get shampoo from the store", "Today at 3:49pm", true),
                  Item("Go to college class", "Today at 3:49pm", true),
                  Item("Work on the ToDo app", "Today at 5:27pm", false),
                  Item("Test this app", "Today at 3:49pm", true),
                ],
              )
            )
          ],
        ),
      ),
    );
  }


  [1]: https://github.com/flutter/flutter/issues/15919

1 个答案:

答案 0 :(得分:0)

使用“构建器”小部件包围FloatingActionButton

return MaterialApp(
  home: Scaffold(
    floatingActionButton: Builder(builder: (context)=> FloatingActionButton(
      backgroundColor: Color(0xff00bbff),
      child: Icon(
        Icons.chat_bubble
      ),
      onPressed: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) => new NewTask()));
        print("test");
      }
    ),),
    body: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SafeArea(
          child: Container(
            child: Padding(
              padding: EdgeInsets.fromLTRB(20, 40, 20, 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "Task List",
                    style: TextStyle(
                      fontWeight: FontWeight.w800,
                      fontSize: 22,
                      color: Color(0xff242424)
                    ),
                  ),
                  Text(
                    "1 incomplete",
                    style: TextStyle(
                      fontWeight: FontWeight.w400,
                      fontSize: 14,
                      color: Color(0xffb3b3b3)
                    ),
                  ),
                ],
              )
            ),
          ),
        ),
        Expanded(
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[

            ],
          )
        )
      ],
    ),
  ),
);