当我尝试使用“文本”窗口小部件呈现item
时,我收到一条错误消息,说class AddCashForm extends StatefulWidget {
@override
_AddCashFormState createState() => _AddCashFormState();
}
class _AddCashFormState extends State<AddCashForm> {
DateTime selectedDate = DateTime.now();
List<String> chipList = [
"one",
"two",
"three",
"four",
];
void dispose() {
oneController.dispose();
twoController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
ChoiceChipWidget(chipList, (item) {
print('Item selected: $item');
return (item);
}),
],
),
RaisedButton(
child: Text("Submit"),
onPressed: () {
},
),
// Text(item), data should appear here
],
),
);
}
}
未定义。
如何在按下按钮时检索值并将其放在小部件中?
PYTHON_CONFIGURE_OPTS="--enable-framework" pyenv install 3.7.4
答案 0 :(得分:1)
如果您希望该项目自动更新,而无需单击提交,请使用以下方法:
class _AddCashFormState extends State<AddCashForm> {
final selectedDate = DateTime.now();
final chipList = <String>[
'one',
'two',
'three',
'four',
];
String item;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: chipList
.map<Widget>((s) => ChoiceChip(
label: Text(s),
selected: s == item,
onSelected: (bool selected) {
setState(() {
item = selected ? s : null;
});
},
))
.toList(),
),
Text(item ?? ''),
],
),
);
}
}
答案 1 :(得分:0)
您可以创建一个字段 selectedItem ,该字段在选择项目时会更新。用 onPressed 方法更新状态。像这样:
class AddCashForm extends StatefulWidget {
@override
_AddCashFormState createState() => _AddCashFormState();
}
class _AddCashFormState extends State<AddCashForm> {
DateTime selectedDate = DateTime.now();
String selectedItem;
List<String> chipList = [
"one",
"two",
"three",
"four",
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
ChoiceChipWidget(chipList, (item) {
print('Item selected: $item');
selectedItem = item;
return (item);
})
],
),
RaisedButton(
child: Text("Submit"),
onPressed: () {
setState(() {});
},
),
// Text(item), data should appear here
],
),
);
}
}