如何使用下拉小部件使常见或自定义的下拉表单字段或在Flutter中下拉列表?

okxuctiv  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(162)

我是一个新的Flutter。我有三个下拉表单字段。一个是状态字段,其API有状态ID和状态名称。第二个是城市字段,其API有城市ID和城市名称。在选择状态时,状态ID在城市字段中传递为原始状态,特定的状态引用在城市下拉列表中。第三个是社会用户角色字段,其API有ID和用户角色,如总统,成员等。我想使用dropdownwidget在flutter中使用常用或自定义下拉表单字段或下拉列表在这里有用,可以在项目中的许多地方使用重复下拉代码。请提供完整的代码帮助。
使共同或自定义下拉表单字段或下拉在Flutter使用下拉小部件

kiz8lqtg

kiz8lqtg1#

class CustomDropDown extends StatelessWidget {
  final double? height;
  final double? width;
  final String hint;
  List<String> dropDownList;
  String selectedValue;
  ValueChanged? onChanged;
  Widget? leading;

  CustomDropDown({
    this.height,
    this.width,
    this.leading,
    required this.hint,
    required this.selectedValue,
    required this.onChanged,
    required this.dropDownList,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height ?? 55,
      width: width ?? double.infinity,
      decoration: BoxDecoration(
        color: Colors.white60,
        border: Border.all(
          width: 1.0,
          color: Colors.white.withOpacity(0.1),
        ),
        borderRadius: BorderRadius.circular(12),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Center(
        child: Row(
          children: [
            leading ?? const SizedBox(),
            SizedBox(width: leading != null ? 10 : 0),
            Expanded(
              child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                  iconDisabledColor: Colors.grey,
                  hint: Text(
                    hint,
                  ),
                  selectedItemBuilder: (_) {
                    return dropDownList
                        .map(
                          (e) => Align(
                            alignment: Alignment.centerLeft,
                            child: Text(
                              e,
                              textAlign: TextAlign.center,
                            ),
                          ),
                        )
                        .toList();
                  },
                  isExpanded: true,
                  icon: const Icon(
                    Icons.keyboard_arrow_down,
                    color: Colors.grey,
                  ),
                  borderRadius: BorderRadius.circular(10),
                  items: dropDownList.map((String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(
                        value,
                      ),
                    );
                  }).toList(),
                  value: selectedValue == '' ? null : selectedValue,
                  onChanged: onChanged,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

像这样使用…

CustomDropDown(
  hint: 'Select Level',
  selectedValue: selectedLevel,// "selectedLevel" is a variable
  dropDownList: const ['High','Low','Medium'],
  onChanged: (value) {
    selectedLevel = value;
  },
),

相关问题