如何解决相关的DropdownButtons并引发onChange事件
我有两个DropdownButton
小部件。第二个内容取决于第一个选择。第二个DropdownButton
将启动第三个小部件的刷新。当第一个DropdownButton
填充后,如何启动第二个DropdownButton
的刷新?然后,当第二个
class ParentBloc {
Stream<List<Parent>> get items => _controller.asyncMap(...);
Future<List<Parent>> _callApi() {
// call endpoint /parents
}
}
class ChildBloc {
ChildBloc(this.parentId);
int parentId;
Stream<List<Child>> get items => _controller.asyncMap(...);
Future<List<Child>> _callApi() {
// call endpoint /parents/$parentId/children
}
}
// This bloc created at init state
ParentBloc parentBloc;
// This bloc will be created only after value will
// be selected in the Parent dropdownbutton because
// I need to know `parentId`.
ChildBloc childBloc;
@override
void initState() {
super.initState();
parentBloc = ParentBloc();
}
@override
Widget build(BuildContext context) {
return Row(
children: [
StreamBuilder<List<Parent>>(
stream: parentBloc.items,builder: (context,snapshot) {
return DropdownButton(
items: snapshot.data.map((item) {
return DropdownButtonItem();
}),);
}
),// Content of this widget depends on above one
StreamBuilder<List<Child>>(
stream: childBloc.items,// Content of this widget depends on above one
StreamBuilder<List<Grandchild>>(
stream: grandchildBloc.items,snapshot) {
return ListView(),]
);
}
也被填充时,如何刷新第三个窗口小部件?
display: flex
解决方法
假设您正在StatefulWidget
内执行此操作,则可以在其中一个函数内使用setState
,在此您可以更新变量,这些变量又必须用来控制每个变量中当前显示的内容您的小部件。
它应该看起来像这样(在您的下拉列表中):
onChanged: (newValue) {
setState(() {
_currentSelection = newValue;
});
},
更新:在评论中进行讨论之后,这是一个有效的示例,我针对如何根据在保管箱内选择的值来进行更新进行了希望,
import 'package:flutter/material.dart';
class ExampleWidget extends StatefulWidget {
@override
_ExampleWidgetState createState() => _ExampleWidgetState();
}
class _ExampleWidgetState extends State<ExampleWidget> {
List<String> someStringsToSelectFrom = [
'value1','value2','value3',];
String selectedValue;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Text('Select something:'),DropdownButton<String>(
value: selectedValue,icon: Icon(
Icons.arrow_downward,color: Colors.deepPurpleAccent,),iconSize: 24,elevation: 16,style: TextStyle(
fontSize: 30.0,color: Colors.green,underline: Container(
height: 2,onChanged: (String newValue) {
setState(() {
selectedValue = newValue;
});
},items: someStringsToSelectFrom.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,child: Text(value),);
}).toList(),Text('This will update after you selected a value:'),someStringsToSelectFrom.contains(selectedValue) ? Text(selectedValue + ' was selected') :
Text('Still waiting for user action.'),],);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。