Flutter-我想将变量从一类传递到另一类 [英] Flutter - I want to pass variable from one class to another

查看:396
本文介绍了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);
              }
            ),
          ],
        )
      ),
    );
  }

查看完整的导航演示

I just want to pass the int and boolean from one class to another class. For that particular integer can be displayed in the app bar of the second page, background color needs to be changed based on boolean (True/false).

解决方案

In navigator you can pass data or object which you want to send to other class.

For example,

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

In SecondScreenWithData class, you can get passed data as below.

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);
              }
            ),
          ],
        )
      ),
    );
  }

Check full Navigation Demo

这篇关于Flutter-我想将变量从一类传递到另一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆