如何使Flutter中的下拉菜单可见

huangapple go评论66阅读模式
英文:

How to make dropdown visibility in flutter

问题

I have a case where I want to make a dropdown containing options where when the user selects the KHS option, a new dropdown will appear below containing the INT list.

我有一个情况,我想创建一个包含选项的下拉菜单,当用户选择KHS选项时,下面会出现一个包含INT列表的新下拉菜单。

how do i make it i am a bit confused here.

我应该如何做呢,我有点困惑。

maybe someone here can help me or have a recommendation for an article I've been looking for but haven't found anything suitable.

也许这里有人可以帮助我,或者推荐一篇我一直在寻找但没有找到合适内容的文章。

Thank you in advance.

提前谢谢。

initialization

初始化

List listItem = [
'Transkrip Sementara',
'KHS',
];
List listItem2 = [
1,
2,
3,
4,
5,
6,
7,
8,
];

in widget

在小部件中

const SizedBox(
height: 8,
),
Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Colors.grey,
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: const Text('Pilih Jenis Transkrip'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue!;
selectSmt = null!;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
Visibility(
visible: selectedValue != null,
child: DropdownButton(
value: selectSmt,
items: listItem2
.where((element) => element?.contains(selectedValue))
.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value as String),
);
}).toList(),
onChanged: (value) {
setState(() {
selectSmt = value!;
});
},
),
),

and an error occurred

发生了错误

如何使Flutter中的下拉菜单可见

what I want

我想要的效果

如何使Flutter中的下拉菜单可见

如何使Flutter中的下拉菜单可见

英文:

I have a case where I want to make a dropdown containing options where when the user selects the KHS option, a new dropdown will appear below containing the INT list.
how do i make it i am a bit confused here.
maybe someone here can help me or have a recommendation for an article I've been looking for but haven't found anything suitable.

Thank you in advance.

> initialization

 List listItem = [
    'Transkrip Sementara',
    'KHS',
  ];
  List<int> listItem2 = [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
  ];

> in widget

const SizedBox(
              height: 8,
            ),
            Container(
              width: double.infinity,
              height: 50,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(
                  color: Colors.grey,
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: DropdownButtonHideUnderline(
                  child: DropdownButton<String>(
                    hint: const Text('Pilih Jenis Transkrip'),
                    value: selectedValue,
                    onChanged: (newValue) {
                      setState(() {
                        selectedValue = newValue!;
                        selectSmt = null!;
                      });
                    },
                    items: listItem.map((valueItem) {
                      return DropdownMenuItem<String>(
                        value: valueItem,
                        child: Text(
                          valueItem,
                        ),
                      );
                    }).toList(),
                  ),
                ),
              ),
            ),
            Visibility(
              visible: selectedValue != null,
              child: DropdownButton<int>(
                value: selectSmt,
                items: listItem2
                    .where((element) => element?.contains(selectedValue))
                    .map((value) {
                  return DropdownMenuItem<int>(
                    value: value,
                    child: Text(value as String),
                  );
                }).toList(),
                onChanged: (value) {
                  setState(() {
                    selectSmt = value!;
                  });
                },
              ),
            ),

and an error occurred
如何使Flutter中的下拉菜单可见

> what I want

如何使Flutter中的下拉菜单可见

如何使Flutter中的下拉菜单可见

答案1

得分: 2

使用map会更容易。关键点是在更改主下拉菜单时重置第二个下拉菜单。

class DropDS extends StatefulWidget {
  const DropDS({super.key});

  @override
  State<DropDS> createState() => _DropDSState();
}

class _DropDSState extends State<DropDS> {
  List<int> listItem1 = List.generate(4, (index) => index + 10);
  List<int> listItem2 = [1, 2, 3, 4, 5, 6, 7, 8];

  // 使用 map 来轻松访问
  late final dropDownData = {
    'Transkrip Sementara': listItem1,
    'KHS': listItem2
  };

  String? selectedValue; // 主类型
  var selectSmt; // 用于 listItem1 和 listItem2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Container(
          width: double.infinity,
          height: 50,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            border: Border.all(
              color: Colors.grey,
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
                hint: const Text('Pilih Jenis Transkrip'),
                value: selectedValue,
                onChanged: (newValue) {
                  setState(() {
                    selectedValue = newValue!;
                    selectSmt = null;
                  });
                },
                items: dropDownData.keys.map((valueItem) {
                  return DropdownMenuItem<String>(
                    value: valueItem,
                    child: Text(valueItem),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
        Visibility(
          visible: selectedValue != null,
          child: DropdownButton<int>(
            value: selectSmt,
            items: dropDownData[selectedValue]?.map((value) {
              return DropdownMenuItem<int>(
                value: value,
                child: Text(value.toString()),
              );
            }).toList(),
            onChanged: (value) {
              selectSmt = value;
              setState(() {});
            },
          ),
        ),
      ],
    ));
  }
}

如果你有任何其他问题,欢迎提出。

英文:

It will be easier while using map. The keypoint is resetting second dropdown while changing main dropdown.

class DropDS extends StatefulWidget {
  const DropDS({super.key});

  @override
  State&lt;DropDS&gt; createState() =&gt; _DropDSState();
}

class _DropDSState extends State&lt;DropDS&gt; {
  List&lt;int&gt; listItem1 = List.generate(4, (index) =&gt; index + 10);
  List&lt;int&gt; listItem2 = [1, 2, 3, 4, 5, 6, 7, 8];

  //lets use map for easy access
  late final dropDownData = {
    &#39;Transkrip Sementara&#39;: listItem1,
    &#39;KHS&#39;: listItem2
  };

  String? selectedValue; // main type
  var selectSmt; // for listItem1 and listItem2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: [
        Container(
          width: double.infinity,
          height: 50,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            border: Border.all(
              color: Colors.grey,
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: DropdownButtonHideUnderline(
              child: DropdownButton&lt;String&gt;(
                hint: const Text(&#39;Pilih Jenis Transkrip&#39;),
                value: selectedValue,
                onChanged: (newValue) {
                  setState(() {
                    selectedValue = newValue!;
                    selectSmt = null;
                  });
                },
                items: dropDownData.keys.map((valueItem) {
                  return DropdownMenuItem&lt;String&gt;(
                    value: valueItem,
                    child: Text(valueItem),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
        Visibility(
          visible: selectedValue != null,
          child: DropdownButton&lt;int&gt;(
            value: selectSmt,
            items: dropDownData[selectedValue]?.map((value) {
              return DropdownMenuItem&lt;int&gt;(
                value: value,
                child: Text(value.toString()),
              );
            }).toList(),
            onChanged: (value) {
              selectSmt = value;
              setState(() {});
            },
          ),
        ),
      ],
    ));
  }
}

huangapple
  • 本文由 发表于 2023年4月4日 15:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定