简体   繁体   English

我想在 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

Here I am getting an error in Hint, when I pass HintName in hint it throws an error, as well I would like to know how can I call a particular list in parameter for example if I want to call the custom DropDownButton with MDPackageType list and hint 'Package Type' how we can implement this.在这里,我在提示中遇到错误,当我在提示中传递 HintName 时它会引发错误,我也想知道如何调用参数中的特定列表,例如,如果我想使用 MDPackageType 列表调用自定义 DropDownButton 和提示“包类型”我们如何实现它。

This is my code这是我的代码

import 'package:flutter/material.dart';

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

  CustomDropDown(this.HintName, 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: const Text(
                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(),
            ),
          ),
        );
      },
    );
  }


[![My expected output][1]][1]}

What is the error message you're getting?您收到的错误消息是什么? One possible issue is that you have const in hint property, try removing that first.一个可能的问题是您在 hint 属性中有 const,请先尝试删除它。 Also, u can try widget.Hintname, instead of Hintname此外,您可以尝试 widget.Hintname,而不是 Hintname

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM