Reputation: 61
I started to work with DropdownButton widget and I created List of cities.
I want to see AlertDialog when i choose my favorite city on the list but it can't work.
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" . Where is the error? Where should i put the bool function? I tried to put this bool function out of setState function but it didn't be as i want.
Upvotes: 0
Views: 1459
Reputation: 870
If you want to show alert dialog you should use 'showDialog', then in its builder use your Alert Dialog.
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: () => {},
)
],
);
},
);
}
}
Upvotes: 1