父下拉列表的选择更改后,如何更新子下拉列表?

时间:2019-02-14 12:00:55

标签: flutter widget

因此,我有一列下拉列表,每个下拉列表都是下面的下拉列表的先决条件。当Dropdown A更改其值时,我将进行API调用并填充Dropdown BDropdown C的选项取决于B的值,依此类推...

我很好地完成了此方案,但是我遇到的问题是选择了下拉菜单A到D,并且我更改了Dropdown B的值时,下拉菜单C和D并没有改变它的值。 / p>

SOS和TY

1 个答案:

答案 0 :(得分:0)

请参阅以下示例:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';

@override
Widget build(BuildContext context) {
return MaterialApp(
  title: _title,
  home: Scaffold(
    appBar: AppBar(title: const Text(_title)),
    body: Center(
      child: MyStatefulWidget(),
    ),
  ),
);
}
}

 /// This is the stateful widget that the main application instantiates.
 class MyStatefulWidget extends StatefulWidget {
 MyStatefulWidget({Key key}) : super(key: key);

 @override
 _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
 }

 /// This is the private State class that goes with MyStatefulWidget.
 class _MyStatefulWidgetState extends State<MyStatefulWidget> {
 String dropdownValue = 'One';

 @override
 Widget build(BuildContext context) {
 return DropdownButton<String>(
  value: dropdownValue,
  icon: Icon(Icons.arrow_downward),
  iconSize: 24,
  elevation: 16,
  style: TextStyle(color: Colors.deepPurple),
  underline: Container(
    height: 2,
    color: Colors.deepPurpleAccent,
  ),
  onChanged: (String newValue) {
    setState(() {
      dropdownValue = newValue;
    });
  },
  items: <String>['One', 'Two', 'Free', 'Four']
      .map<DropdownMenuItem<String>>((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
);

} }