gpt4 book ai didi

android - Flutter-将数据从 child 传递到 parent 和另一个 child

转载 作者:行者123 更新时间:2023-12-03 04:58:56 25 4
gpt4 key购买 nike

我有一个类(class)限制和3个 child 。

我想将数据从CountryDropDown(和看上去几乎相同的FieldDropDown类,以及从DateSelector类,但可能稍后)传递给Restrictions,然后传递给UserOffers(通过单击按钮更新List)。

现在,我只能在Restrictions类中定义变量并像这样传递它。所有关于字符串。

限制类别:

  String selectedCountry = "Argentina"; 
...

children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20),
child: FieldDropDown(),
),
Container(
padding: EdgeInsets.only(top: 10),
margin: EdgeInsets.only(left: 20),
child: CountryDropDown(
),
...

Container(
child: UserOffers(
country: selectedCountry,
field: selectedField,
),
),

UserOffers类:
class UserOffers extends StatefulWidget {

final String country;
final String field;

UserOffers({
this.country,
this.field
}); ...

CountryDropDown类:
  static String value;
static String selected;

@override
void initState() {
selected = _CountryDropDownState.selected;
super.initState();
} ...
... ]
.map((label) => DropdownMenuItem(
child: Text(label),
value: label,
))
.toList(),
onChanged: (value) {
setState(() => selected = value);
print(selected);
},
),
),
)

先感谢您。

最佳答案

看来您的问题与Flutter中的state management有关。有几种方法可以解决此问题,例如:

  • 继承了小部件
  • 提供者
  • redux

  • 如果您想了解更多信息,请检查 official doumentation

    关于android - Flutter-将数据从 child 传递到 parent 和另一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60002836/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com