我是flutter框架的新手,我对dropdownbutton有一些问题。如何将选定的下拉按钮保存到mysql数据库
这段代码实际上可以保存到mysqldb中作为文本字段。但是如何获得选择的下拉按钮值
class FormOrtuView extends StatefulWidget {
@override
_FormOrtuViewState createState() => _FormOrtuViewState();
}
class _FormOrtuViewState extends State<FormOrtuView> {
String _dropdownValueAgm = 'Katolik';
final _key = new GlobalKey<FormState>();
插入数据库代码:
sumbit(String namaAyah,agamaAyah) async {
var ayah = {
"id_register": idReg,
"nama_ayah": namaAyah,
"agama_ayah": agamaAyah,
};
final response = await http.post(BaseUrl.ayah, body: {
'id_register': ayah['id_register'],
'nama_ayah': ayah['nama_ayah'],
'agama_ayah': ayah['agama_ayah'],
});
print(response.statusCode);
final data = jsonDecode(response.body);
print(data);
}
显示下拉按钮值的代码
DropdownButton<String> _buildDropdownButtonAgama() {
return DropdownButton<String>(
value: _dropdownValueAgm,
onChanged: (String agamaAyah) {
setState(() {
_dropdownValueAgm = agamaAyah;
});
},
items: <String>[
'Katolik',
'Kristen',
'Islam',
'Hindu',
'Buddha',
'Kong Hu Chu',
'Kepercayaan'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
返回代码以显示ui
List<DemoItem<dynamic>> _demoItems;
@override
void initState() {
super.initState();
getPref();
return Form(
key: _key,
child: Builder(
builder: (BuildContext context) {
return CollapsibleBody(
margin: const EdgeInsets.symmetric(horizontal: 16.0),
onSave: () {
Form.of(context).save();
close();
_sumbit(namaAyah, agamaAyah);
},
onCancel: () {
Form.of(context).reset();
close();
},
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
hintText: "ex. John john",
labelText: "Nama",
),
onSaved: (e) => namaAyah = e,
),
下拉按钮代码
Container(
margin: EdgeInsets.only(bottom: 10.0),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
side:
BorderSide(color: Colors.grey, width: 1.0),
borderRadius: BorderRadius.circular(10.0)),
),
width: 320.0,
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: _buildDropdownButtonAgama(),
),
),
),
],
);
},
),
最佳答案
我认为您可以在提交数学方法时尝试以下代码:-
sumbit(String namaAyah,agamaAyah) async {
var ayah = {
"id_register": idReg,
"nama_ayah": namaAyah,
"agama_ayah": _dropdownValueAgm, //here i have change.
};
final response = await http.post(BaseUrl.ayah, body: {
'id_register': ayah['id_register'],
'nama_ayah': ayah['nama_ayah'],
'agama_ayah': ayah['agama_ayah'],
});
print(response.statusCode);
final data = jsonDecode(response.body);
print(data);
}