简体   繁体   English

我想在 flutter 中创建一个自定义 DropdownButton,它可以与不同的列表一起重复使用 [Flutter]

[英]I want to create a custom DropdownButton in flutter which can be reusable with different List [Flutter]

Here I have four Lists as in the code below and is it possible to call the list through parameter od any someother way for reusable purpose, example if I have four DropDown Button and each DropDown Button contains different Lists so how i can create a reusable code for implement this.这里我有四个列表,如下面的代码所示,是否可以通过参数 od 以任何其他方式调用列表以实现可重用目的,例如,如果我有四个 DropDown Button 并且每个 DropDown Button 包含不同的列表,那么我如何创建可重用代码为了实现这一点。

Here is what i tried,这是我试过的,

import 'package:flutter/material.dart';

class CustomDropDown extends StatefulWidget {
  final String HintName;
  final List<String> MDPT;

  CustomDropDown({required this.HintName, required this.MDPT});
  @override
  _CustomDropDownState createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
  String? dropdownvalue;
  var MDPackageType = [
    'Package Type 1',
    'Package Type 2',
    'Package Type 3',
    'Package Type 4',
  ];
  var MDPackageName = [
    'Package Name 1',
    'Package Name 2',
    'Package Name 3',
    'Package Name 4',
  ];

  var PTPackageType = [
    'Package Type 1',
    'Package Type 2',
    'Package Type 3',
    'Package Type 4',
  ];
  var PTPackageName = [
    'Package Type 1',
    'Package Type 2',
    'Package Type 3',
    'Package Type 4',
  ];

  @override
  Widget build(BuildContext context) {
    return FormField<String>(
      builder: (FormFieldState<String> state) {
        return InputDecorator(
          decoration: InputDecoration(
            // labelStyle: textStyle,

            // errorStyle: TextStyle(color: Colors.redAccent, fontSize: 16.0),
            hintText: 'Package Type',
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(5.0),
            ),
          ),
          isEmpty: dropdownvalue == '',
          child: DropdownButtonHideUnderline(
            child: DropdownButton<String>(
              hint: Text(
                widget.HintName,
                style: TextStyle(color: Colors.blue),
              ),
              value: dropdownvalue,
              isDense: true,
              onChanged: (String? newValue) {
                setState(() {
                  dropdownvalue = newValue;
                  state.didChange(newValue);
                });
              },

              
              items: MDPT.map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(
                    value,
                    style: TextStyle(color: Colors.blue),
                  ),
                );
              }).toList(),
            ),
          ),
        );
      },
    );
  }

我的预期输出

Use the following function to build DropdownMenuItem list using a List .使用以下 function 使用List构建DropdownMenuItem列表。

List<DropdownMenuItem> buildDropdownMenuItems(List list){
      List<DropdownMenuItem> dropDownItems = [];
      list.forEach((value) {
        dropDownItems.add(
            DropdownMenuItem<String>(
              value: value,
              child: Text(
                value,
                style: TextStyle(color: Colors.blue),
              ),
            )
        );
      });
      
      return dropDownItems;
    }

Use items: buildDropdownMenuItems(MDPT) property to call the function.使用items: buildDropdownMenuItems(MDPT)属性调用 function。

So the full code as below.所以完整的代码如下。

import 'package:flutter/material.dart';

class CustomDropDown extends StatefulWidget {
  final String hintName;
  final List<String> MDPT;

  CustomDropDown({required this.hintName, required this.MDPT});
  @override
  _CustomDropDownState createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
  String? dropdownvalue;

  List<DropdownMenuItem<String>> buildDropdownMenuItems(List list){
    List<DropdownMenuItem<String>> dropDownItems = [];
    list.forEach((value) {
      dropDownItems.add(
          DropdownMenuItem<String>(
            value: value,
            child: Text(
              value,
              style: TextStyle(color: Colors.blue),
            ),
          )
      );
    });

    return dropDownItems;
  }

  @override
  Widget build(BuildContext context) {
    return FormField<String>(
      builder: (FormFieldState<String> state) {
        return InputDecorator(
          decoration: InputDecoration(
            hintText: widget.hintName,
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(5.0),
            ),
          ),
          isEmpty: dropdownvalue == '',
          child: DropdownButtonHideUnderline(
            child: DropdownButton<String>(
              hint: Text(
                widget.hintName,
                style: TextStyle(color: Colors.blue),
              ),
              value: dropdownvalue,
              isDense: true,
              onChanged: (String? newValue) {
                setState(() {
                  dropdownvalue = newValue;
                });
              },

              //call buildDropdownMenuItems() here and pass the list of Strings as attribue
              items: buildDropdownMenuItems(widget.MDPT),
            ),
          ),
        );
      },
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在 flutter 中创建一个可重复使用的自定义 DropdownButton,这里提示名称和列表对于所有 DropdownButton 都是不同的 - I want to create a custom DropdownButton in flutter which can be reusable, here the hint name and list will be different for all DropdownButton 如何使用 JSON 数据列表创建 DropdownButton,我希望它在 Flutter 中填充我的 DropDownButton - How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter 无法使用 Flutter 中的对象列表创建 DropdownButton - Can't create DropdownButton with List of objects in Flutter 在 Flutter 中为 DropdownButton 创建项目列表 - Create Item list for DropdownButton in Flutter 我想用 flutter 创建一个可以排序的列表 - I want to use flutter to create a list that can be sorted 数据表中的 Flutter DropdownButton,列表中的 DropdownButton 选项 - Flutter DropdownButton in DataTable, DropdownButton options from list Flutter - 我想创建自定义 TextFomrField 边框 - Flutter - I want to create a custom TextFomrField border 如何在 flutter 中创建自定义可重用警报对话框 - How to create custom reusable alert dialog in flutter 如何使用颤振中的列表中的 JSON 数据列表创建 DropdownButton - How to Create DropdownButton with a list of JSON data within a list in flutter 我想在 flutter 中创建用户配置文件列表? - i want to create a list of user profile in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM