[英]How to pass Drop down menu value to another func or widget? Flutter/Dart
我有 2 個 arguments、標題和 ID 的下拉菜單。 下拉所選項目將僅顯示標題 arguments 但我想將標題的 id 傳遞給另一個小部件,如未來的構建器,所以有什么想法嗎?
DropdownButton<String>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(
left: MediaQuery.of(context).size.width / 20),
child: Text(
'انتخاب موقعیت',
style: TextStyle(fontFamily: "IranSans"),
),
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: sos.map((lst lst1) {
return DropdownMenuItem<String>(
value: lst1.title,
child: Text(lst1.title),
);
}).toList(),
),
class lst{
String title;
int id;
lst({this.id, this.title});
}
List<lst> sos = <lst>[
lst(id: 2, title: 'شهرک سعدی'),
lst(id: 3, title: 'وکیل - طفریه - مردوخ'),
lst(id: 4, title: 'شهرک ۵ آذر'),
lst(id: 5, title: 'بلوار شبلی'),
]
從您提供的示例中,我看到您正在使用字符串創建下拉列表,但您應該使用示例中提到的列表 object 創建它。 我已經為您創建了一個示例示例,請查看它並告訴我它是否有效
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String countryValue;
List<String> countriesList = [];
List<String> cityList = [];
String cityValue;
@override
void initState() {
super.initState();
}
List<lst> sos = <lst>[
lst(id: 2, title: 'شهرک سعدی'),
lst(id: 3, title: 'وکیل - طفریه - مردوخ'),
lst(id: 4, title: 'شهرک ۵ آذر'),
lst(id: 5, title: 'بلوار شبلی'),
];
lst dropdownValue;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Users List "),
),
body: Container(
width: 300,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DropdownButton<lst>(
value: dropdownValue,
//icon: const Icon(Icons.arrow_downward,),
iconSize: 22,
elevation: 16,
style: TextStyle(color: Colors.black),
underline: Container(
height: 2,
color: Colors.black,
),
isDense: true,
hint: Padding(
padding: EdgeInsets.only(
left: MediaQuery.of(context).size.width / 20),
child: Text(
'انتخاب موقعیت',
style: TextStyle(fontFamily: "IranSans"),
),
),
onChanged: (lst newValue) {
setState(() {
dropdownValue = newValue;
});
print(dropdownValue.id);
print(dropdownValue.title);
},
items: sos.map((lst lst1) {
return DropdownMenuItem<lst>(
value: lst1,
child: Text(lst1.title),
);
}).toList(),
),
],
),
),
);
}
}
class lst {
String title;
int id;
lst({this.id, this.title});
}
現在,當您 select 下拉時,您將獲得 id 和標題,您可以將其傳遞給另一個小部件。 讓我知道它是否有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.