<p>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.</p> <p>This is how the final output is going to look.</p> <p> async { if (vi >= 1) { await new Future.delayed(const Duration(seconds: 1), () ); return 'Hello World ' + vi.toString(); } return null; }</p> <p>void main() => runApp(MyApp());</p> <p>class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); }</p> <p>class _MyAppState extends State { int visible = 0;</p> <p>@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: [ 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), ], ), ), ), ); } }</p> <p>class StringWidget extends StatelessWidget { final Future str; final int vi;</p> <p>const StringWidget({Key key, this.str, this.vi}) : super(key: key);</p> <p snapshot.error="" snapshot.data="">@override Widget build(BuildContext context) { return Center( child: FutureBuilder( future: str, builder: (context, AsyncSnapshot 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: $'); else if (snapshot.hasData) return Text('$'); else return Text('Press button to start.'); } return Text('Press button to start.'); // unreachable }, ), ); } } Hope this is helpful to you.</p> <p>Thanks, Srikanth</p>
How to show a CircularProgressIndicator on Button Click
This Article is posted by seven.srikanth at 2/8/2019 5:45:12 AM
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter
Tags: show a CircularProgressIndicator on Button Click; Loading while async;
Check out our other latest articles
Safearea widget - How to avoid visual overlap with Notch on flutter mobile app?How to convert row of widgets into column of widgets in flutter based on screen size?
How to run Flutter programs from GitHub?
How to get screen orientation in flutter?
NavigationRail example in flutter