How to show a CircularProgressIndicator on Button Click

This Article is posted by seven.srikanth at 08-02-2019 05:45:12

In this example, I'm going to share the code which will show a CircularProgressIndicator when a Button is clicked and return the results to the screen once the operations are completed. 

This is how the final output is going to look.

Below is an example code of how to Show CircularProgressIndicator on Button Click in Flutter. Code from Main.dart file. In order to run this project locally, all you need to do is to create a Project and copy paste the below code to Lib\main.dart file.

import 'dart:async';
import 'package:flutter/material.dart';

Future<String> fetchStr(int vi) async {
if (vi >= 1) {
await new Future.delayed(const Duration(seconds: 1), () {});
return 'Hello World ' + vi.toString();
return null;

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

class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();

class _MyAppState extends State<MyApp> {
int visible = 0;

Widget build(BuildContext context) {
return MaterialApp(
title: 'Loader Example',
theme: ThemeData(
home: Scaffold(
appBar: AppBar(
title: Text('Fetch Data Example'),
body: Center(
child: Column(
children: <Widget>[
padding: EdgeInsets.symmetric(vertical: 100.0),
onPressed: () {
setState(() {
child: Text('Click here to Load'),
padding: EdgeInsets.symmetric(vertical: 10.0),
StringWidget(str: fetchStr(visible), vi: visible),

class StringWidget extends StatelessWidget {
final Future<String> str;
final int vi;

const StringWidget({Key key, this.str,}) : super(key: key);

Widget build(BuildContext context) {
return Center(
child: FutureBuilder<String>(
future: str,
builder: (context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.waiting:
return CircularProgressIndicator();
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else if (snapshot.hasData)
return Text('${}');
return Text('Press button to start.');
return Text('Press button to start.'); // unreachable

Hope this is helpful to you.


Tags: show a CircularProgressIndicator on Button Click; Loading while async;

Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.

Recent Articles

Open-Source News App built with Flutter

Posted by nishantkotatech on 26 Feb 2020 Article

Meme Generator with Flutter

Posted by theindianappguy on 25 Feb 2020 Tutorials

Flutter Scaffold Widget

Posted by themaaz32 on 05 Feb 2020 Tutorials

Flutter Tensorflow Tutorial

Posted by theindianappguy on 28 Jan 2020 Tutorials

© 2018 - Fluttercentral | Email to me -