[英]How to get DropdownButtonFormField value with a button click - Flutter
[英]How to add Flutter DropdownButtonFormField
初學 Flutter 愛好者,剛學習小部件系統。 想要使用開箱即用的小部件(不是插件可以)實現自動完成文本字段 DropdownButtonFormField 非常適合我的用例,但是當我嘗試使用它時,編譯器給了我一個 Method not found 錯誤。
Compiler message:
lib/expanding_text.dart:100:11: Error: Method not found: 'DropdownButtonFormField'.
DropdownButtonFormField(),
^^^^^^^^^^^^^^^^^^^^^^^
lib/expanding_text.dart:100:11: Error: The method 'DropdownButtonFormField' isn't defined for the class '#lib1::_TripItemState'.
Try correcting the name to the name of an existing method, or defining a method named 'DropdownButtonFormField'.
DropdownButtonFormField(),
這是我的代碼(相關部分)
import 'package:flutter/material.dart';
...
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
DropdownButtonFormField<String>(
items: [DropdownMenuItem<String>(child:Text("test"))],
),
查看文檔,似乎我可以自由地將它添加到小部件樹中而無需額外配置。 但顯然,由於錯誤,我在這里遺漏了一些東西。
因此,為了解決發生的問題,DropdownButtonFormField 是否仍在材料庫中?
還有什么我想念的嗎?
該小部件確實存在於flutter/materials.dart
。
DropdownButtonFormField
需要在其構造函數中定義items
屬性。 你需要像這樣使用它:
import 'package:flutter/material.dart';
...
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
DropdownButtonFormField(
items: <DropdownMenuItem>[
// Put widgets in the drop down menu here
],
)
]);
}
該小部件確實存在於 flutter/materials.dart 中。
import 'package:flutter/material.dart';
...
String _selectedValue;
List<String> listOfValue = ['1', '2', '3', '4', '5'];
@override
Widget build(BuildContext context) {
return DropdownButtonFormField(
value: _selectedValue,
hint: Text(
'choose one',
),
isExpanded: true,
onChanged: (value) {
setState(() {
_selectedValue = value;
});
},
onSaved: (value) {
setState(() {
_selectedValue = value;
});
},
validator: (String value) {
if (value.isEmpty) {
return "can't empty";
} else {
return null;
}
},
items: listOfValue
.map((String val) {
return DropdownMenuItem(
value: val,
child: Text(
val,
),
);
}).toList(),
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.