Flutter 在Flatter中单击DropdownButton值后主页不刷新
我在DropdownButton的onChanged()中调用Future方法,试图重新加载主页,但页面没有重新加载 以下是主页小部件代码:-Flutter 在Flatter中单击DropdownButton值后主页不刷新,flutter,model-view-controller,Flutter,Model View Controller,我在DropdownButton的onChanged()中调用Future方法,试图重新加载主页,但页面没有重新加载 以下是主页小部件代码:- class HomeWidget extends StatefulWidget { final GlobalKey<ScaffoldState> parentScaffoldKey; String city_id; String cityName; HomeWidget({Key key, this.parentScaf
class HomeWidget extends StatefulWidget {
final GlobalKey<ScaffoldState> parentScaffoldKey;
String city_id;
String cityName;
HomeWidget({Key key, this.parentScaffoldKey, this.city_id, this.cityName})
: super(key: key);
@override
_HomeWidgetState createState() => _HomeWidgetState();
}
class _HomeWidgetState extends StateMVC<HomeWidget> {
HomeController _con;
_HomeWidgetState() : super(HomeController()) {
_con = controller;
}
void initState() {
widget.city_id = _con.city_id;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: new IconButton(
icon: new Icon(Icons.sort, color: Theme.of(context).hintColor),
onPressed: () => widget.parentScaffoldKey.currentState.openDrawer(),
),
automaticallyImplyLeading: false,
backgroundColor: Colors.transparent,
elevation: 0,
centerTitle: true,
title: ValueListenableBuilder(
valueListenable: settingsRepo.setting,
builder: (context, value, child) {
return Text(
value.appName ?? S.of(context).home,
style: Theme.of(context)
.textTheme
.title
.merge(TextStyle(letterSpacing: 1.3)),
);
},
),
actions: <Widget>[
new ShoppingCartButtonWidget(
iconColor: Theme.of(context).hintColor,
labelColor: Theme.of(context).accentColor),
],
),
body: RefreshIndicator(
onRefresh: _con.refreshHome,
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: SearchBarWidget(
onClickFilter: (event) {
widget.parentScaffoldKey.currentState.openEndDrawer();
},
),
),
SizedBox(
height: 10.0,
),
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 110.0,
height: 50.0,
child: DropdownButtonHideUnderline(
child: DropdownButton(
value: widget.cityName,
items: _con.cityData
.map((Data cities) => DropdownMenuItem<String>(
value: cities.id.toString(),
child: Text(
cities.name,
style: TextStyle(fontSize: 15.0),
),
))
.toList(),
onChanged: (String value) {
setState(() {});
print(
"Value id :::::::::::::::::::>>>>>>>>>>>>>>>> " +
value);
for (int i = 0; i < _con.cityData.length; i++) {
if (_con.cityData[i].id.toString() == value) {
_con.city_id = value;
widget.cityName = value;
}
}
_con.refreshHome(
id: value, cityName: widget.cityName);
},
),
),
),
),
),
SizedBox(height: 10.0),
Container(
margin: const EdgeInsets.all(10.0),
child: ClipRect(
/** Banner Widget **/
child: Banner(
message: "20% off !!",
location: BannerLocation.bottomStart,
color: Colors.red,
child: Container(
color: Colors.green[100],
height: 150,
child: Padding(
padding: EdgeInsets.fromLTRB(10, 20, 10, 20),
child: Column(
children: <Widget>[
Image.network(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190806131525/forkPython.jpg'), //Image.network
SizedBox(height: 10),
Text(
'GeeksforGeeks',
style: TextStyle(
color: Colors.green,
fontSize: 40,
fontWeight: FontWeight.bold), //TextStyle
),
SizedBox(
height: 5,
), //SizedBox
Text(
'Fork Python Course',
style: TextStyle(
color: Colors.green,
fontSize: 20,
fontWeight: FontWeight.bold), //TextStyle
), //Text
SizedBox(height: 20),
RaisedButton(
child: Text('Register'),
color: Colors.greenAccent[400],
onPressed: () {},
) //RaisedButton
], //<Widget>[]
), //Column
), //Padding
), //Container
), //Banner
), //ClipRect
), //container
SizedBox(
height: 10.0,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: ListTile(
dense: true,
contentPadding: EdgeInsets.symmetric(vertical: 0),
leading: Icon(
Icons.category,
color: Theme.of(context).hintColor,
),
title: Text(
S.of(context).category,
style: Theme.of(context).textTheme.display1,
),
),
),
CategoriesCarouselWidget(
categories: _con.categories,
city_id: _con.city_id,
),
ListTile(
dense: true,
contentPadding: EdgeInsets.symmetric(horizontal: 20),
leading: Icon(
Icons.trending_up,
color: Theme.of(context).hintColor,
),
title: Text(
S.of(context).trending_this_week,
style: Theme.of(context).textTheme.display1,
),
subtitle: Text(
S.of(context).double_click_on_the_food_to_add_it_to_the,
style: Theme.of(context)
.textTheme
.caption
.merge(TextStyle(fontSize: 11)),
),
),
FoodsCarouselWidget(
foodsList: _con.trendingFoods, heroTag: 'home_food_carousel'),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20, bottom: 20),
child: ListTile(
dense: true,
contentPadding: EdgeInsets.symmetric(vertical: 0),
leading: Icon(
Icons.trending_up,
color: Theme.of(context).hintColor,
),
title: Text(
S.of(context).most_popular,
style: Theme.of(context).textTheme.display1,
),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: GridWidget(
restaurantsList: _con.popularRestaurants,
heroTag: 'home_restaurants',
),
),
],
),
),
),
);
}
}
class HomeWidget扩展了StatefulWidget{
最终GlobalKey parentScaffoldKey;
字符串城市标识;
字符串cityName;
HomeWidget({Key,this.parentScaffoldKey,this.city\u id,this.cityName})
:super(key:key);
@凌驾
_HomeWidgetState createState();
}
类_homewigetstate扩展StateMVC{
家庭控制器;
_HomeWidgetState():超级(HomeController()){
_con=控制器;
}
void initState(){
widget.city\u id=\u con.city\u id;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
领先:新图标按钮(
图标:新图标(Icons.sort,color:Theme.of(context.hintColor),
onPressed:()=>widget.parentScaffoldKey.currentState.openDrawer(),
),
自动嵌入:false,
背景颜色:颜色。透明,
海拔:0,
标题:对,
标题:ValueListenableBuilder(
valueListenable:settingsRepo.setting,
生成器:(上下文、值、子级){
返回文本(
value.appName??S.of(context).home,
风格:主题(上下文)
.文本主题
标题
.合并(文本样式(字母间距:1.3)),
);
},
),
行动:[
新购物车按钮Widget(
iconColor:Theme.of(context).hintColor,
labelColor:Theme.of(context.accentColor),
],
),
正文:刷新指示器(
onRefresh:_con.refreshHome,
子:SingleChildScrollView(
填充:边集。对称(水平:0,垂直:10),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.start,
mainAxisSize:mainAxisSize.max,
儿童:[
填充物(
填充:常量边集。对称(水平:20),
子:SearchBarWidget(
onClickFilter:(事件){
widget.parentScaffoldKey.currentState.openEndDrawer();
},
),
),
大小盒子(
身高:10.0,
),
对齐(
对齐:alignment.centerLeft,
孩子:填充(
填充:常数边集全部(8.0),
子:容器(
宽度:110.0,
身高:50.0,
子项:DropdownButtonHideUnderline(
孩子:下拉按钮(
值:widget.cityName,
项目:_con.cityData
.map((数据城市)=>下拉菜单项(
值:cities.id.toString(),
子:文本(
城市名称,
样式:TextStyle(fontSize:15.0),
),
))
.toList(),
onChanged:(字符串值){
setState((){});
印刷品(
“值id::+
价值);
对于(int i=0;i<_con.cityData.length;i++){
if(_con.cityData[i].id.toString()==值){
_con.city_id=值;
widget.cityName=值;
}
}
_康宁之家酒店(
id:value,cityName:widget.cityName);
},
),
),
),
),
),
尺寸箱(高度:10.0),
容器(
边距:所有常数边集(10.0),
孩子:ClipRect(
/**横幅小部件**/
孩子:横幅(
信息:“八折!!”,
位置:BannerLocation.bottomStart,
颜色:颜色,红色,
子:容器(
颜色:颜色。绿色[100],
身高:150,
孩子:填充(
填充:来自LTRB(10,20,10,20)的边缘设置,
子:列(
儿童:[
图像网络(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20190806131525/forkPython.jpg“),//Image.network
尺寸箱(高度:10),
正文(
“Geeksforgeks”,
样式:TextStyle(
颜色:颜色。绿色,
尺寸:40,
fontWeight:fontWeight.bold),//TextStyle
),
大小盒子(
身高:5,,
),//大小框
正文(
“霍克Python课程”,
样式:TextStyle(
颜色:颜色。绿色,
尺寸:20,
fontWeight:fontWeight.bold),//TextStyle
),//文本
尺寸箱(高度:20),
升起的按钮(
子:文本(‘寄存器’),
颜色:颜色。绿色口音[400],
按下:(){},
)//升起按钮
], //[]
),//列
),//填充
),//容器
),//横幅
),//ClipRect
),//容器
大小盒子(
身高:10.0,
),
填充物(
填充:常量边集。对称(hor)
class HomeController extends ControllerMVC {
List<Category> categories = <Category>[];
List<Restaurant> topRestaurants = <Restaurant>[];
List<Restaurant> popularRestaurants = <Restaurant>[];
List<Review> recentReviews = <Review>[];
List<Food> trendingFoods = <Food>[];
List<Data> cityData = <Data>[];
String city_id;
String city_name;
HomeController() {
selectCity();
listenForTrendingFoods();
listenForCategories();
listenForPopularRestaurants();
listenForRecentReviews();
}
Future<void> selectCity() async {
final Stream<Data> stream = await getCitiesList();
stream.listen((Data city) {
setState(() => cityData.add(city));
}, onError: (a) {
print("Error" + a.toString());
}, onDone: () {
setState(()=>{});
city_id = cityData[0].id.toString();
city_name = cityData[0].name;
print("Done Cities ::::::::::>>>>>>> " + cityData.toString());
});
}
Future<void> listenForCategories() async {
final Stream<Category> stream = await getCategories();
stream.listen((Category _category) {
setState(() => categories.add(_category));
}, onError: (a) {
print(a);
}, onDone: () {});
}
Future<void> listenForTopRestaurants(String city_id) async {
final Stream<Restaurant> stream = await getNearRestaurants(
deliveryAddress.value, deliveryAddress.value, city_id);
stream.listen((Restaurant _restaurant) {
setState(() => topRestaurants.add(_restaurant));
}, onError: (a) {
print("Api Error");
}, onDone: () {
print("Api Success");
});
}
Future<void> listenForPopularRestaurants() async {
final Stream<Restaurant> stream =
await getPopularRestaurants(deliveryAddress.value,city_id);
stream.listen((Restaurant _restaurant) {
setState(() => popularRestaurants.add(_restaurant));
}, onError: (a) {}, onDone: () {});
}
Future<void> listenForRecentReviews() async {
final Stream<Review> stream = await getRecentReviews();
stream.listen((Review _review) {
setState(() => recentReviews.add(_review));
}, onError: (a) {}, onDone: () {});
}
Future<void> listenForTrendingFoods({String city_id}) async {
final Stream<Food> stream =
await getTrendingFoods(deliveryAddress.value, city_id);
stream.listen((Food _food) {
setState(() => trendingFoods.add(_food));
}, onError: (a) {
print(a);
}, onDone: () {});
}
void requestForCurrentLocation(BuildContext context) {
OverlayEntry loader = Helper.overlayLoader(context);
Overlay.of(context).insert(loader);
setCurrentLocation().then((_address) async {
deliveryAddress.value = _address;
await refreshHome();
loader.remove();
}).catchError((e) {
loader.remove();
});
}
Future<void> refreshHome({String id, String cityName}) async {
setState(() {
categories = <Category>[];
topRestaurants = <Restaurant>[];
popularRestaurants = <Restaurant>[];
recentReviews = <Review>[];
trendingFoods = <Food>[];
cityData = <Data>[];
city_id = id;
city_name = cityName;
refresh();
});
await listenForTopRestaurants(id);
await listenForTrendingFoods(city_id: id);
await listenForCategories();
await listenForPopularRestaurants();
await listenForRecentReviews();
}
}