gpt4 book ai didi

dart - Flutter:下拉列表中的选定值

转载 作者:IT王子 更新时间:2023-10-29 06:44:07 26 4
gpt4 key购买 nike

当我选择一个项目时,对话框中的初始值不会改变。这是下拉列表的代码:

void _buildStatusDialog(String documentID) {
String _selectedText = "SDD";
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Status Update"),
content: new DropdownButton<String>(
hint: Text("Status"),
value: _selectedText,
items: <String>['SDD', 'Meeting', 'Home', 'Space']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String val) {
_selectedText = val;
setState(() {
_selectedText = val;
});
},
),
actions: <Widget>[
FlatButton(
child: Text("UPDATE"),
onPressed: () {
.....
},
),
],
);
});

如何更新“提示”或显示所选项目?

enter image description here

最佳答案

根据@Jonah Williams 在评论中的提示,我想出了以下工作示例来解决我遇到的类似问题:

import 'package:flutter/material.dart';

class StatusDialog extends StatefulWidget {

@override
State<StatefulWidget> createState() {
return StatusDialogState();
}
}

class StatusDialogState extends State<StatusDialog> {
String _selectedText = "SSD";

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("Status Update"),
content: new DropdownButton<String>(
hint: Text("Status"),
value: _selectedText,
items: <String>['SDD', 'Meeting', 'Home', 'Space']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String val) {
setState(() {
_selectedText = val;
});
},
),
actions: <Widget>[
FlatButton(
child: Text("UPDATE"),
onPressed: () {
//.....
},
),
],
);
}
}

void _buildStatusDialog(String documentID) {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return StatusDialog();
}
);
}

然后您只需要添加一些逻辑以从 StatusDialog 获取 _selectedText - 可能使用回调。

关于dart - Flutter:下拉列表中的选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51489861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com