skip to Main Content

I tried to show a circular progress indicator while loading when clicked on the elevated button here I used setState to update the state, but somehow state is not getting updated.
I didn’t understand why it’s not getting updated any mistake in the below code?
is there something that I can improve my code please help me out thanks in Advance.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 
  bool isLoading = false;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: ElevatedButton(
                    onPressed: () async {
                  setState(() {
                    isLoading == true;
                  });
                  await Future.delayed(const Duration(seconds: 5));
                  setState(() {
                    isLoading = false;
                  });
               
                },
                  child: FittedBox(
                  child: isLoading 
                      ? CircularProgressIndicator(
                    color: Colors.white,
                  )
                      : Row(
                    children: const [
                      Text(
                        "Submit",
                      ),
                      SizedBox(
                        width: 2,
                      ),
                      Icon(Icons.check_circle),
                    ],
                  ),
                ),
                  style: ElevatedButton.styleFrom(
                    primary: Colors.yellow,
                    minimumSize: const Size(100, 40),
                    elevation: 3,
                  )
                ),
      ),
      
    );
  }
}

2

Answers


  1. Brother you make small mistake in

        setstate(){
           IsLoding == !isloading;
        }
    

    You wrote == insted of =

    Login or Signup to reply.
  2. Just change == to = in set state

    onPressed: () async {
                  setState(() {
                    isLoading == true; -----> isLoading = true;
                  });
                  await Future.delayed(const Duration(seconds: 5));
                  setState(() {
                    isLoading = false;
                  });
               
                },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search