Flutter searchable_dropdown Ui在单击按钮时不更新,并且在按钮的onPressed()内部调



我正在创建2个可搜索下拉列表,并在单击图标按钮时交换2个下拉列表的值,这些值正在交换,但当我在按钮的onPressed((内调用setState((函数时,ui不会改变:

以下是我的代码:pubspec.yaml依赖项:searchable_dropdown:^1.1.3

import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.dart';

class MainDashBoardTab extends StatefulWidget {
@override
_MainDashBoardTabState createState() => _MainDashBoardTabState();
}
class _MainDashBoardTabState extends State<MainDashBoardTab> {
String fromPlaceSelected = "";
String toPlaceSelected = "";
List<String> fromPlaceItemsString = [
"Borivali",
"Kandivali",
"Malad",
"Andheri",
"Dadar"
];
List<String> toPlaceItemsString = [
"Kankavli",
"Pune",
"Solapur",
"Manmad",
"Belgaum",
"Kolhapur"
];
List<DropdownMenuItem> items = [];
List<DropdownMenuItem> items1 = [];
@override
void initState() {
super.initState();
for (int i = 0; i < fromPlaceItemsString.length; i++) {
items.add(DropdownMenuItem(
child: Text(fromPlaceItemsString[i]),
value: fromPlaceItemsString[i],
));
fromPlaceSelected = fromPlaceItemsString[fromPlaceItemsString.length - 1];
}
for (int i = 0; i < toPlaceItemsString.length; i++) {
items1.add(DropdownMenuItem(
child: Text(toPlaceItemsString[i]),
value: toPlaceItemsString[i],
));
toPlaceSelected = toPlaceItemsString[toPlaceItemsString.length - 1];
}
}
fromPlaceWidget() {
return SearchableDropdown.single(
items: items,
value: fromPlaceSelected,
hint: "Select From Place ",
searchHint: "Select From Place",
onChanged: (value) {
setState(() {
fromPlaceSelected = value;
});
},
isExpanded: true,
);
}
toPlaceWidget() {
return SearchableDropdown.single(
items: items1,
value: toPlaceSelected,
hint: "Select To Place",
searchHint: "Select To Place",
onChanged: (value) {
setState(() {
toPlaceSelected = value;
});
},
isExpanded: true,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: EdgeInsets.all(8),
color: Colors.lightBlue[100],
height: 250,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
fromPlaceWidget(),
toPlaceWidget(),
],
),
),
Expanded(
flex: 1,
child: IconButton(
icon: Icon(Icons.swap_vert),
iconSize: 50,
color: Colors.blue,
onPressed: () {
setState(() {
String temp = fromPlaceSelected;
fromPlaceSelected = toPlaceSelected;
toPlaceSelected = temp;
});
},
),
)
],
),

],
),
),
);
}
}

因为您有两个不同的列表。因此,即使在交换了值之后,新值也不会出现在列表中。您还必须交换列表,或者更好地交换整个小部件。

相关内容

  • 没有找到相关文章

最新更新