Home > OS >  Alert dialog state not changing outside the function in flutter
Alert dialog state not changing outside the function in flutter

Time:04-28

When I tried to change the state from outside the function the state is not changing.

void _showAlertDialog(BuildContext context) {
    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: const Text("Diabetes Prediction"),
          content: StatefulBuilder(
              return _predictedResult == "" ? Column(
            mainAxisSize: MainAxisSize.min,
            children: [
               const CircularProgressIndicator(),
               Text(loadingText),
            ],
          ) : Text(_predictedResult);
            },
            ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            TextButton(
              child: const Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

Elevated button helps in calling the _showAlertDialog the loadingText is declared inside the class

ElevatedButton(
              child: const Text("Predict"),
              onPressed: () async {
                // Pregnancies, Glucose, Blood Pressure, Insulin, Skin Thickness, Pedigree Function, Weight,
                // Height, Age
                _predictedResult = "";
                loadingText = "";
                var data = _formatData();
                var result = Serializer().serialize(data);
                _showAlertDialog(context);
                setState(() {
                  loadingText = "Sending data to server...";
                });
                await Future.delayed(const Duration(seconds: 2), (){
                  
                });
                setState(() {
                  loadingText = "Analyzing data...";
                  });
                // await Future.delayed(const Duration(seconds: 2), (){
                //   print("data received");
                  
                // });
                await _predict(result);
                
              },
            ),

The output comes as Sending data to server...

CodePudding user response:

  String _predictedResult = '';
  StreamController<String>? controller;
  String loadingText = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[900],
      body: Center(
        child: ElevatedButton(
          child: const Text("Predict"),
          onPressed: () async {
            controller = StreamController<String>();
            // Pregnancies, Glucose, Blood Pressure, Insulin, Skin Thickness, Pedigree Function, Weight,
            // Height, Age
            _predictedResult = "";
            loadingText = "";
            _showAlertDialog(context);
            controller!.add("Sending data to server...");

            await Future.delayed(const Duration(seconds: 2), () {});
            controller!.add("Analyzing data...");

            await Future.delayed(const Duration(seconds: 2), () {
              print("data received");
            });

            controller!.add("data received!");
          },
        ),
      ),
    );
  }

  void _showAlertDialog(BuildContext context) {
    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: const Text("Diabetes Prediction"),
          content: StreamBuilder(
            stream: controller!.stream,
            builder: (context, AsyncSnapshot<String> snap) {
              return _predictedResult == ""
                  ? Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        const CircularProgressIndicator(),
                        Text(snap.data ?? "Loading..."),
                      ],
                    )
                  : Text(_predictedResult);
            },
          ),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            TextButton(
              child: const Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

Use Stream

Flutter Stream Basics for Beginners

  • Related