在下拉框中未选择数值。

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

Value not selected in dropdown flutter

问题

我尝试在Flutter中创建一个下拉菜单,并根据Flutter的文档进行了调整,也尝试了一篇文章中的方法,但当我在我的应用程序中尝试选择其中一个值时,我想要的值不会出现,我必须再次单击,然后新选择的数据才会出现。
如何解决这个问题?

List listItem = [
  'Transkrip Sementara',
  'KHS',
];
String? selectedValue;

// 在Widget中

return Scaffold(
  appBar: AppBar(
    backgroundColor: primaryColor,
    title: const Center(
      child: Text(
        'Transkrip Nilai',
      ),
    ),
    actions: [
      IconButton(
        icon: const Icon(Icons.print),
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return Dialog(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                elevation: 5,
                child: SizedBox(
                  width: double.infinity,
                  child: Padding(
                    padding: const EdgeInsets.all(14),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Center(
                          child: Text(
                            'Cetak Transkrip/KHS',
                            style: bold5,
                          ),
                        ),
                        const Divider(),
                        Text(
                          'Pilih Jenis Transkrip',
                          style: regular5,
                        ),
                        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;
                                  });
                                },
                                items: listItem.map((valueItem) {
                                  return DropdownMenuItem<String>(
                                    value: valueItem,
                                    child: Text(
                                      valueItem,
                                    ),
                                  );
                                }).toList(),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              );
            },
          );
        },
      ),
    ],
  ),
);

请检查上述代码,以确保与您的Flutter应用程序的其他部分兼容,并确保已正确处理下拉菜单的值更改。

英文:

I tried to make a dropdown in Flutter and I have adjusted it to the documentation from Flutter and also tried from the article but when I try it in my application when I select one of the values I want it doesn't appear, I have to click again and the new selected data appears.
how do i solve this?

在下拉框中未选择数值。

 List listItem = [
&#39;Transkrip Sementara&#39;,
&#39;KHS&#39;,
];
String? selectedValue;

> in widget

return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: const Center(
child: Text(
&#39;Transkrip Nilai&#39;,
),
),
actions: [
IconButton(
icon: const Icon(Icons.print),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: 5,
child: SizedBox(
width: double.infinity,
child: Padding(
padding: const EdgeInsets.all(14),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Text(
&#39;Cetak Transkrip/KHS&#39;,
style: bold5,
),
),
const Divider(),
Text(
&#39;Pilih Jenis Transkrip&#39;,
style: regular5,
),
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;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem&lt;String&gt;(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),

答案1

得分: 1

使用 StatefulBuilder

参考此链接:https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html

showDialog(
  context: context,
  builder: (context) {
    return Dialog(
       shape: RoundedRectangleBorder(
       borderRadius: BorderRadius.circular(8),
       child: StatefulBuilder( // 将此部分添加到您的代码中
        builder: (BuildContext context, StateSetter setState) {
          return 您的对话框内容
.....    
英文:

use StatefulBuilder

read this for reference: https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html

showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
child: StatefulBuilder( // add this to your code
builder: (BuildContext context, StateSetter setState) {
return Your content dialog
.....

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

发表评论

匿名网友

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

确定