【问题标题】:how to navigated to other page in flutter by clicking on button? [duplicate]如何通过单击按钮导航到颤振中的另一个页面? [复制]
【发布时间】:2020-08-23 05:38:10
【问题描述】:
Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        body: Container(
          padding: EdgeInsets.fromLTRB(10.0, 50.0, 10.0, 10.0),
          child: Column(
            children: [
              TextField(
                controller: user,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    hintText: "ENTER USER NAME"),
              ),
              SizedBox(
                height: 10.0,
              ),
              TextField(
                controller: pass,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    hintText: "ENTER USER NAME"),
              ),
              FlatButton(
                  onPressed: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => Home()));
                  },
                  child: Text("NAVIGATOR"))
            ],
          ),
        ),
      ),
    );
  }

在 navigator.push 中出现错误

“这是我在媒体上遇到的错误” 处理手势时抛出以下断言:

使用不包含 Navigator 的上下文请求的导航器操作。用于从 Navigator 推送或弹出路由的上下文必须是作为 Navigator 小部件后代的小部件的上下文。

【问题讨论】:

标签: flutter


【解决方案1】:
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
  body: Container(
    padding: EdgeInsets.fromLTRB(10.0, 50.0, 10.0, 10.0),
    child: Column(
      children: [
        TextField(
          controller: user,
          decoration: InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              hintText: "ENTER USER NAME"),
        ),
        SizedBox(
          height: 10.0,
        ),
        TextField(
          controller: pass,
          decoration: InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              hintText: "ENTER USER NAME"),
        ),
        FlatButton(
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => Home()));
            },
            child: Text("NAVIGATOR"))
      ],
    ),
  ),
)
);
}

请编写这样的代码,无论何时对任何平台提出疑问

也请提供Home()的代码

这是 Home() 的更改代码

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  HomeState createState() => HomeState();
}

class HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
     return Scaffold(
        body: Container(
          child: Column(
            children: [
              Text(
                "WELCOME",
                style: TextStyle(fontSize: 50.0),
              )
            ],
          ),
        ),
      );
  }
}

【讨论】:

  • 怎样才能像你写的那样写代码??Aman Verma
  • 我已经更改了您主屏幕的代码,它会起作用,如果有帮助,请将其标记为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多