如何在卡片中将数据从卡传递到新页面

时间:2019-02-23 19:32:32

标签: dart flutter

我想将数据从卡传递到新页面,该数据有两个名称START(开始),并在STOP(停止)下方。当用户单击卡时,“ START”数据应传递到新页面。下面是代码< / p>

class MyList extends StatefulWidget {
  @override
  _MyListState createState() => _MyListState();
}

class _MyListState extends State<MyList> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text("List"),
      ),
      body: new Container(
        padding: new EdgeInsets.only(left: 5.0,top: 20.0,right: 5.0),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            GestureDetector(
              onTap: (){

              },
              child: new Card(
                child:
                new Column(
                  children: <Widget>[
                    new Text('START'),
                    new Text('STOP')
                  ],
                ),
              ),
            ),
  

新页面

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("New page"),
      ),
      body:Container(
child:Center(
new Text('textview'))

1 个答案:

答案 0 :(得分:1)

您的新页面MyHomePage应该接受所需的输入作为参数,例如

class MyHomePage extends StatefulWidget {
  final String startText;
  final String stopText;
  MyHomePage(this.startText, this.stopText);
  ....
}
.....

onTap应该使用此数据创建MyHomePage的新实例,并像这样启动页面

Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => MyHomePage("start", "stop"),
      ),
    );