您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Flutter-我想将变量从一类传递到另一类

Flutter-我想将变量从一类传递到另一类

在导航器中,您可以将要发送的数据或对象传递给其他类。

例如,

// Data need to sent second screen
class Person {
  final String name;
  final String age;

  Person(this.name, this.age);
}

// Navigate to second screen with data
Navigator.push(context, new MaterialPageRoute(builder: (context) => new SecondScreenWithData(person: new Person("Priyank","28"))));

SecondScreenWithData课堂上,您可以通过以下方式获取传递的数据。

class SecondScreenWithData extends StatelessWidget {
  // Declare a field that holds the Person data
  final Person person;

  // In the constructor, require a Person
  SecondScreenWithData({Key key, @required this.person}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Second Screen With Data"),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            // Display passed data from first screen
            new Text("Person Data  \nname: ${person.name} \nage: ${person.age}"),
            new RaisedButton(
              child: new Text("Go Back!"),
              onPressed: () {
                // Navigate back to first screen when tapped!
                Navigator.pop(context);
              }
            ),
          ],
        )
      ),
    );
  }

查看完整的导航演示

其他 2022/1/1 18:17:17 有694人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶