Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android studio – AlertDialog widget not working setState function in Flutter

Posted on November 11, 2022 By

Solution 1 :

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: () => {},
            )
          ],
        );
      },
    );
  }
}

Problem :

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,


        ),
      ],
    );

  }
}

View of my page :
enter image description here

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.

READ  [FIXED] android - problem in configureFlutterEngine method writing kotlin code in flutter
Powered by Inline Related Posts

Comments

Comment posted by user229044

All content posted here must be in English.

Android Tags:android-studio, flutter, setstate, visual-studio-code, widget

Post navigation

Previous Post: [FIXED] android – How to achieve ionic permissions on first load
Next Post: [FIXED] android – How do i put online database into local database

Related Posts

[FIXED] java – Getting NullPointerException when inputting text in an EditText Android
[FIXED] Android R8 fragments implements but actually matches extends Android
[FIXED] How to change background color of number picker in android Android
[FIXED] android – java.io.FileNotFoundException: open failed: EISDIR (Is a directory) Android
[FIXED] java – How to make selected text on EditText underlined? Android
[FIXED] android – AndroidStudio error: null value in entry: workingDir=null Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme