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

我只想将int和boolean从一个类传递给另一个类。对于可以在第二页的应用程序栏中显示的特定整数,需要根据布尔值(真/假)更改背景色。

回答:

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

例如,

// 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);

}

),

],

)

),

);

}

查看完整的导航演示

以上是 Flutter-我想将变量从一类传递到另一类 的全部内容, 来源链接: utcz.com/qa/415744.html

回到顶部