Flutter 颤振-使用相同的提供程序更新下拉列表并将数据发送到另一个小部件
我想将所选值从下拉列表传递给其父级,然后通过构造函数传递给子级,然后通过setState函数更新列表 所以,我希望从下拉列表中选择的值可以在限制(UserOffers)的子项中访问 将数据从父类发送到子类实际上是可行的,但仅当我通过构造函数将父类中定义的数据传递给子类时=我不确定这是否是一种好方法 现在我只使用提供者更新下拉列表上的数据,但我不知道如何传递它 CountryProvider类:Flutter 颤振-使用相同的提供程序更新下拉列表并将数据发送到另一个小部件,flutter,mobile,dart,provider,state-management,Flutter,Mobile,Dart,Provider,State Management,我想将所选值从下拉列表传递给其父级,然后通过构造函数传递给子级,然后通过setState函数更新列表 所以,我希望从下拉列表中选择的值可以在限制(UserOffers)的子项中访问 将数据从父类发送到子类实际上是可行的,但仅当我通过构造函数将父类中定义的数据传递给子类时=我不确定这是否是一种好方法 现在我只使用提供者更新下拉列表上的数据,但我不知道如何传递它 CountryProvider类: class CountryProvider with ChangeNotifier { List&
class CountryProvider with ChangeNotifier {
List<String> _items = [
"Argentina",
"Brazil",
...
"Spain",
"Switzerland",
];
String _selectedItem;
List<String> get items => _items;
String get selected => _selectedItem;
void setSelectedItem(String s) {
_selectedItem = s;
notifyListeners();
}
}
class CountryDropDown extends StatefulWidget {
@override
_CountryDropDownState createState() => _CountryDropDownState();
}
class _CountryDropDownState extends State<CountryDropDown> {
@override
void initState() {
super.initState();
}
...
ChangeNotifierProvider<CountryProvider>(
create: (context) => CountryProvider(),
child: Container(
width: 215,
child: Consumer<CountryProvider>(
builder: (_, provider, __) {
return DropdownButton<String>(
hint: Text("Not selected"),
icon: Icon(Icons.flight),
value: dropdownValue,
onChanged: (String newValue) {
provider.setSelectedItem(newValue);
dropdownValue = provider.selected;
print(newValue);
},
items: provider.items
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
...
class Restrictions extends StatefulWidget {
@override
_RestrictionsState createState() => _RestrictionsState();
}
class _RestrictionsState extends State<Restrictions> {
@override
void initState() {
super.initState();
}
...
Column(
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: DateSelector(),
),
],
),
Container(
child: UserOffers(
country: selectedCountry,
field: selectedField,
),
...
带有ChangeNotifier的类CountryProvider{
列表项=[
“阿根廷”,
“巴西”,
...
“西班牙”,
“瑞士”,
];
字符串_selectedItem;
列表获取项=>\u项;
字符串get selected=>\u selectedItem;
void setSelectedItem(字符串s){
_选择editem=s;
notifyListeners();
}
}
国家下拉列表类:
class CountryProvider with ChangeNotifier {
List<String> _items = [
"Argentina",
"Brazil",
...
"Spain",
"Switzerland",
];
String _selectedItem;
List<String> get items => _items;
String get selected => _selectedItem;
void setSelectedItem(String s) {
_selectedItem = s;
notifyListeners();
}
}
class CountryDropDown extends StatefulWidget {
@override
_CountryDropDownState createState() => _CountryDropDownState();
}
class _CountryDropDownState extends State<CountryDropDown> {
@override
void initState() {
super.initState();
}
...
ChangeNotifierProvider<CountryProvider>(
create: (context) => CountryProvider(),
child: Container(
width: 215,
child: Consumer<CountryProvider>(
builder: (_, provider, __) {
return DropdownButton<String>(
hint: Text("Not selected"),
icon: Icon(Icons.flight),
value: dropdownValue,
onChanged: (String newValue) {
provider.setSelectedItem(newValue);
dropdownValue = provider.selected;
print(newValue);
},
items: provider.items
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
...
class Restrictions extends StatefulWidget {
@override
_RestrictionsState createState() => _RestrictionsState();
}
class _RestrictionsState extends State<Restrictions> {
@override
void initState() {
super.initState();
}
...
Column(
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: DateSelector(),
),
],
),
Container(
child: UserOffers(
country: selectedCountry,
field: selectedField,
),
...
class CountryDropDown扩展了StatefulWidget{
@凌驾
_CountryDropDownState createState()=>\u CountryDropDownState();
}
类\u CountryDropDownState扩展状态{
@凌驾
void initState(){
super.initState();
}
...
变更通知提供者(
创建:(上下文)=>CountryProvider(),
子:容器(
宽度:215,
儿童:消费者(
生成器:(uu,提供者,uu){
返回下拉按钮(
提示:文本(“未选择”),
图标:图标(Icons.flight),
value:dropdownValue,
onChanged:(字符串newValue){
provider.setSelectedItem(newValue);
dropdownValue=provider.selected;
打印(新值);
},
项目:provider.items
.map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
);
...
和家长:
限制类:
class CountryProvider with ChangeNotifier {
List<String> _items = [
"Argentina",
"Brazil",
...
"Spain",
"Switzerland",
];
String _selectedItem;
List<String> get items => _items;
String get selected => _selectedItem;
void setSelectedItem(String s) {
_selectedItem = s;
notifyListeners();
}
}
class CountryDropDown extends StatefulWidget {
@override
_CountryDropDownState createState() => _CountryDropDownState();
}
class _CountryDropDownState extends State<CountryDropDown> {
@override
void initState() {
super.initState();
}
...
ChangeNotifierProvider<CountryProvider>(
create: (context) => CountryProvider(),
child: Container(
width: 215,
child: Consumer<CountryProvider>(
builder: (_, provider, __) {
return DropdownButton<String>(
hint: Text("Not selected"),
icon: Icon(Icons.flight),
value: dropdownValue,
onChanged: (String newValue) {
provider.setSelectedItem(newValue);
dropdownValue = provider.selected;
print(newValue);
},
items: provider.items
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
...
class Restrictions extends StatefulWidget {
@override
_RestrictionsState createState() => _RestrictionsState();
}
class _RestrictionsState extends State<Restrictions> {
@override
void initState() {
super.initState();
}
...
Column(
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: DateSelector(),
),
],
),
Container(
child: UserOffers(
country: selectedCountry,
field: selectedField,
),
...
类限制扩展了StatefulWidget{
@凌驾
_RestrictionsState createState()=>\u RestrictionsState();
}
类限制状态扩展状态{
@凌驾
void initState(){
super.initState();
}
...
纵队(
儿童:[
容器(
页边空白:仅限边缘组(左:20),
子项:FieldDropDown(),
),
容器(
填充:仅限边缘设置(顶部:10),
页边空白:仅限边缘组(左:20),
子项:CountryDropDown(),
),
],
),
容器(
子项:DateSelector(),
),
],
),
容器(
孩子:用户提供(
国家/地区:选择的国家/地区,
字段:selectedField,
),
...
@Boras!你用所有的…
为我做了很多工作
我认为您需要将状态提升到小部件树的更高位置,以便当用户从下拉菜单中选择国家时,UserOffers
可以更改
完成下面的示例应用程序
请注意,我将模型类名称更改为SingleSelectCountry
。该模型类的实例不是提供程序。该模型类的实例是由提供程序提供的。这有一个重要的区别
还要注意,所有小部件都扩展了无状态小部件
。通常使用包提供程序可以避免使用StatefulWidget
导入“dart:collection”;
进口“包装:颤振/材料.省道”;
导入“包:provider/provider.dart”;
//模型---------------------------------------------------
使用ChangeNotifier初始化SingleSelectCountry{
最终清单_项目=[
“阿根廷”,
“比利时”,
“巴西”,
“丹麦”,
“英格兰”,
“法国”,
“芬兰”,
“德国”,
“荷兰”,
“爱尔兰”,
“挪威”,
“波兰”,
“苏格兰”,
“西班牙”,
“瑞典”,
“瑞士”,
“威尔士”,
];
字符串_selectedItem;
不可修改的ListView获取项目{
返回不可修改的ListView(此.\u项);
}
字符串被选中{
返回此项。\u选择编辑项;
}
设置选定项(最终字符串项){
此项。\ u选择编辑项=项目;
this.notifyListeners();
}
}
//用户界面------------------------------------------
void main(){
返回runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(原始样本:颜色。蓝色),
主页:MyHomePage(),
);
}
}
类MyHomePage扩展了无状态小部件{
@凌驾
小部件构建(最终构建上下文){
//提供程序必须位于窗口小部件树中高于
//“CountryDropDown”和“UserOffers”都有。
回程多供应商(
供应商:[
变更通知提供者(
创建:(最终构建上下文){
返回SingleSelectCountry();
},
),
],
孩子:脚手架(
appBar:appBar(
标题:文本(“我的主页”),
),
正文:限制(),
),
);
}
}
类限制扩展了无状态控件{
@凌驾
小部件构建(最终构建上下文){
返回列(
儿童:[
CountryDropDown(),
UserOffers(),
],
);
}
}
类CountryDropDown扩展了无状态小部件{
@凌驾
小部件构建(最终构建上下文){
退货消费者(
建造商:(
最终构建上下文上下文,
最终单选计数