【问题标题】:How to get a variable of .dart file to another .dart file in flutter如何在颤动中将.dart文件的变量获取到另一个.dart文件
【发布时间】:2020-11-28 11:11:57
【问题描述】:
class widget_model extends StatelessWidget {
    final text;
    widget_model(this.text);

    String input = "";
    @override
    Widget build(BuildContext context) {
    return Expanded(
      child: Row(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
    Container(
                  height: 60,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                          colors: [Colors.pink, Colors.purpleAccent])),

                  child: InkWell(
                    onTap: () {
                      print("Pressed one");
                      print("The text is $input");
                      input=input+"1";
                      print("The text after $input");
                      Navigator.push(context, MaterialPageRoute(builder: (BuildContext)=>Homepage(input)));
                    },

                    child: Text(
                      "$text",
                      style: TextStyle(color: Colors.white, fontSize: 50),
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 10,
            width: 5,
          )
        ],
      ),
    );
  }
}

此 .dart 文件中的变量“输入”已在无状态小部件中声明,但如何将此变量获取到另一个 .dart 文件。如何通知变量值的变化。

【问题讨论】:

    标签: android flutter variables dart parameter-passing


    【解决方案1】:

    对于一个干净的方法,如果两个小部件位于树中的不同分支中,您需要一个像 provider 这样的状态管理库来在两个小部件之间共享变量。如果使用相同变量的其他小部件是当前小部件的子部件,则可以在每次输入值更改时使用有状态小部件和 setState。

    【讨论】:

      【解决方案2】:

      你可以通过两种方式做到这一点

      1. 使用来自 pub.dev 的 Provider 包,然后您可以在任何其他 dart 文件中使用该变量。这是比较复杂的程序的首选方式。
      2. 您可以在小部件外部声明和初始化变量,也可以在另一个 dart 文件中使用该变量。 像这样,
      String input = "";
      
      class widget_model extends StatelessWidget {
      final text;
      widget_model(this.text);
      
      @override
      Widget build(BuildContext context) {
      return Expanded(
        child: Row(
          children: <Widget>[
            Expanded(
              child: Column(
                children: <Widget>[
      Container(
                    height: 60,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                            colors: [Colors.pink, Colors.purpleAccent])),
      
                    child: InkWell(
                      onTap: () {
                        print("Pressed one");
                        print("The text is $input");
                        input=input+"1";
                        print("The text after $input");
                        Navigator.push(context, MaterialPageRoute(builder: (BuildContext)=>Homepage(input)));
                      },
      
                      child: Text(
                        "$text",
                        style: TextStyle(color: Colors.white, fontSize: 50),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 10,
              width: 5,
            )
          ],
        ),
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-09
        • 1970-01-01
        • 1970-01-01
        • 2012-10-08
        • 2020-07-17
        • 2020-10-24
        • 1970-01-01
        • 2019-09-11
        相关资源
        最近更新 更多