[英]AlertDialog widget not working setState function in Flutter
I started to work with DropdownButton widget and I created List of cities.我开始使用 DropdownButton 小部件并创建了城市列表。
I want to see AlertDialog when i choose my favorite city on the list but it can't work.当我在列表中选择我最喜欢的城市时,我想看到 AlertDialog 但它不起作用。 Here is codes:这是代码:
import 'package:flutter/material.dart';
import './func.dart';
class ChooseCity extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ChooseCityState();
}
}
class ChooseCityState extends State<ChooseCity> {
var cities = ["Ankara", "İzmir", "İstanbul", "Edirne", "Antalya"];
String choosenCity = "Edirne";
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<String>(
items: cities.map((String city) {
return DropdownMenuItem<String>(
child: Text(city),
value: city,
);
}).toList(),
value: choosenCity,
onChanged: (String choosen) {
setState(() {
choosenCity = choosen;});
choosen=="Antalya" ?
AlertDialog(
title:Text("ATTENTION"),
content: Text("You chose the best place!!"),
actions: [
FlatButton(child: Text("I see,I agree"),onPressed: ()=>{},)
],
)
: Text("go on");
},
),
SizedBox(
height: 60,
),
Text(
" You choosed :" + choosenCity,
textAlign: TextAlign.center,
),
],
);
}
}
It doesn't look like as i want.它看起来不像我想要的那样。 I want to see AlertDialog when i chose "Antalya".当我选择“安塔利亚”时,我想看到 AlertDialog。 Where is the error?错误在哪里? Where should i put the bool function?我应该把布尔 function 放在哪里? I tried to put this bool function out of setState function but it didn't be as i want.我试图把这个 bool function 放在 setState function 之外,但它不是我想要的。
If you want to show alert dialog you should use 'showDialog', then in its builder use your Alert Dialog.如果您想显示警报对话框,您应该使用“showDialog”,然后在其构建器中使用您的警报对话框。
Like this像这样
class ChooseCity extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ChooseCityState();
}
}
class ChooseCityState extends State<ChooseCity> {
var cities = ["Ankara", "İzmir", "İstanbul", "Edirne", "Antalya"];
String chosenCity = "Edirne";
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DropdownButton<String>(
items: cities.map((String city) {
return DropdownMenuItem<String>(
child: Text(city),
value: city,
);
}).toList(),
value: chosenCity,
onChanged: (String choosen) {
chosenCity = choosen;
showAlertDialog();
setState(() {});
},
),
SizedBox(
height: 60,
),
Text(
" You choosed :" + chosenCity,
textAlign: TextAlign.center,
),
],
);
}
showAlertDialog() {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("ATTENTION"),
content: Text("You chose the best place!!"),
actions: [
FlatButton(
child: Text("I see,I agree"),
onPressed: () => {},
)
],
);
},
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.