【问题标题】:How can I put a variable into Text in Flutter?如何将变量放入 Flutter 中的文本中?
【发布时间】:2022-12-15 13:11:56
【问题描述】:

新手的简单问题。我想将使用回调创建的变量的结果保存为 var 并将其输出到另一个小部件的文本中。我应该怎么办..?

                    Row(
                        children: [
                          Expanded(
                            child: Container(
                              height: 40,
                              color: primaryColor20,
                              child: Text(''),
                            ),
                          ),
                          ElevatedButton(
                            onPressed: () async {
                              await Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (_) => KpostalView(
                                    callback: (Kpostal result){
                                      print(result.address);
                                      var resultAddress = result.address;
                                    },
                                  ),
                                ),
                              );
                            },
                            child: Text('Find Address'),
                          ),
                        ],
                      ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    使用StatefulWidget,然后setState(() => resultAddress = result.address;这样的税你可以使用Text(resultAddress)

    【讨论】:

      【解决方案2】:

      你必须使用StatefulWidget

      class MyWidget extends StatefulWidget {
        const MyWidget({Key? key}) : super(key: key);
      
        @override
        State<MyWidget> createState() => _MyWidgetState();
      }
      
      class _MyWidgetState extends State<MyWidget> {
        String resultAddress = ""; //This is the variable that you will use in the expanded
        @override
        Widget build(BuildContext context) {
          return Row(
            children: [
              Expanded(
                child: Container(
                  height: 40,
                  color: primaryColor20,
                  child: resultAddress, //Here is where you use it
                ),
              ),
              ElevatedButton(
                onPressed: () async {
                  await Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (_) => KpostalView(
                        callback: (Kpostal result) {
                          print(result.address);
                          setState(() {
                            //You have to use the set state to rebuild the widget with the new state
                            resultAddress = result.address;
                          });
                        },
                      ),
                    ),
                  );
                },
                child: Text('Find Address'),
              ),
            ],
          );
        }
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用 Text('My text ${result.address}') 但要刷新您需要将其置于 StatefulWidget 中的值

        【讨论】:

          猜你喜欢
          • 2018-07-14
          • 1970-01-01
          • 2021-12-24
          • 1970-01-01
          • 2012-09-20
          • 1970-01-01
          • 1970-01-01
          • 2018-05-07
          • 1970-01-01
          相关资源
          最近更新 更多