Filter by Widgets


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 {
@override
_MyAppState createState() => _MyAppState();
}

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

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Loader Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Fetch Data Example'),
),
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 100.0),
),
RaisedButton(
onPressed: () {
setState(() {
visible++;
});
},
child: Text('Click here to Load'),
),
Padding(
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, this.vi}) : super(key: key);

@override
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.active:
case ConnectionState.waiting:
return CircularProgressIndicator();
case ConnectionState.done:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else if (snapshot.hasData)
return Text('${snapshot.data}');
else
return Text('Press button to start.');
}
return Text('Press button to start.'); // unreachable
},
),
);
}
}


Hope this is helpful to you.

Thanks,
Srikanth

0 Comments
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

Error: The name 'RangeSlider' is defined in the libraries 'package:flutter/src/material/range_slider.dart' and 'package:flutter_range_slider/src/flutter_range_slider.dart'.

Check out this article on Error: The name 'RangeSlider' is defined in the libraries 'package:flutter/src/material/range_slider.dart' and 'package:flutter_range_slider/src/flutter_range_slider.dart'.

Read full post

How to create a persistent bottom sheet in Flutter?

Check out this article on How to create a persistent bottom sheet in Flutter?

Read full post

How to draw a Decorative Horizontal line in Flutter?

Check out this article on How to draw a Decorative Horizontal line in Flutter?

Read full post

How to fetch an Image from Internet in Flutter

Check out this article on How to fetch an Image from Internet in Flutter

Read full post

ListView with Images from the Internet

Check out this article on ListView with Images from the Internet

Read full post

How to show Tooltip in Flutter?

Check out this article on How to show Tooltip in Flutter?

Read full post

How to create BottomNavigationBar in flutter?

Check out this article on How to create BottomNavigationBar in flutter?

Read full post

How to add actions to Appbar?

Check out this article on How to add actions to Appbar?

Read full post

How to open dart devtools in VS code?

Check out this article on How to open dart devtools in VS code?

Read full post

Accessibility features in flutter

Check out this article on Accessibility features in flutter

Read full post

© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License , and code samples are licensed under the BSD License.