在flutter中使用材质按钮加载新页面

时间:2020-08-22 17:19:59

标签: flutter flutter-layout

我使用此代码加载了新页面,但无济于事! 它与StatefulWidget有关吗? (MaterialButton在一个稳定的小部件下)

                          MaterialButton(
                            onPressed: () {
                              titlenote = titleinput.text;
                              subtitlenote = subtitleinput.text;
                              Navigator.of(context).pop();
                              new MaterialPageRoute(builder: (context) {
                                return notes();
                              });
                            },

这是新页面的无固定小部件:

class notes extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new Column(children: <Widget>[
        new Container(
          padding: EdgeInsets.fromLTRB(8, 20, 8, 20),
          color: Colors.transparent,
          child: new CheckboxListTile(
            value: true,
            onChanged: (bool a) {},
            title: Text("$titlenote"),
            selected: true,
            checkColor: Colors.green,
            activeColor: Colors.black,
            tristate: true,
            subtitle: Text("$subtitlenote"),
          ),
          alignment: Alignment.center,
        )
      ]),
    );
  }

}

1 个答案:

答案 0 :(得分:0)

您创建了MaterialPageRoute,但从未使用导航器来推送它。顺便说一句,您可以使用pushReplacement一次弹出推送

Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (context) {
        return notes();
}));

N.B:关键字new自Dart 2起就没有用了,您可以在每次看到它时将其删除。