Loading Indicators
Loading preview
Starting the Flutter app
A showcase of Flutter's built-in loading indicators and spinners
import 'package:flutter/material.dart';
class LoadingDemo extends StatefulWidget {
const LoadingDemo({super.key});
@override
State<LoadingDemo> createState() => _LoadingDemoState();
}
class _LoadingDemoState extends State<LoadingDemo> {
/// Shows a dialog that closes itself after two seconds. Holding the route
/// means the timer can only ever close this dialog: if the user already
/// dismissed it with back, there is nothing left to do.
void _showTimedDialog(WidgetBuilder builder) {
final navigator = Navigator.of(context, rootNavigator: true);
final route = DialogRoute<void>(
context: context,
barrierDismissible: false,
builder: builder,
);
navigator.push(route);
Future.delayed(const Duration(seconds: 2), () {
if (route.isCurrent) {
navigator.pop();
} else if (route.isActive) {
navigator.removeRoute(route);
}
});
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(title: const Text('Loading Indicators')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Circular Progress Indicators',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Wrap(
spacing: 16,
runSpacing: 16,
children: [
const CircularProgressIndicator(),
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
const CircularProgressIndicator(strokeWidth: 4),
const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(strokeWidth: 2),
),
],
),
const SizedBox(height: 32),
const Text(
'Linear Progress Indicators',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Wrap(
spacing: 16,
runSpacing: 16,
children: [
const SizedBox(width: 200, child: LinearProgressIndicator()),
SizedBox(
width: 200,
child: LinearProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
const SizedBox(
width: 200,
child: LinearProgressIndicator(minHeight: 4),
),
SizedBox(
width: 200,
child: LinearProgressIndicator(
value: 0.7,
backgroundColor: scheme.surfaceContainerHighest,
valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
),
),
],
),
const SizedBox(height: 32),
const Text(
'Refresh Indicators',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
SizedBox(
height: 200,
child: RefreshIndicator(
onRefresh: () async {
await Future.delayed(const Duration(seconds: 2));
},
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(title: Text('Item ${index + 1}'));
},
),
),
),
const SizedBox(height: 32),
const Text(
'Loading Overlays',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Wrap(
spacing: 16,
runSpacing: 16,
children: [
ElevatedButton(
onPressed: () => _showTimedDialog(
(context) =>
const Center(child: CircularProgressIndicator()),
),
child: const Text('Show Loading Overlay'),
),
ElevatedButton(
onPressed: () => _showTimedDialog(
(context) => Center(
child: Material(
color: scheme.surfaceContainerHigh,
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text(
'Loading...',
style: TextStyle(color: scheme.onSurface),
),
],
),
),
),
),
),
child: const Text('Show Loading Dialog'),
),
],
),
const SizedBox(height: 32),
const Text(
'Skeleton Loading',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border.all(color: scheme.outlineVariant),
borderRadius: BorderRadius.circular(8),
),
child: Column(
children: [
Container(
width: double.infinity,
height: 20,
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(4),
),
),
const SizedBox(height: 16),
Container(
width: double.infinity,
height: 100,
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(4),
),
),
const SizedBox(height: 16),
Row(
children: [
Container(
width: 100,
height: 20,
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(4),
),
),
const SizedBox(width: 16),
Container(
width: 100,
height: 20,
decoration: BoxDecoration(
color: scheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(4),
),
),
],
),
],
),
),
],
),
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
A catalogue of indicators in one scrolling Column. LoadingDemo is stateful but holds no state, and the RefreshIndicator only waits two seconds. Both overlays go through _showTimedDialog, which keeps its DialogRoute and closes that route after a fake delay, so the timer can’t pop the page if back already dismissed the dialog. ‘Skeleton Loading’ is static Containers with no shimmer, coloured from colorScheme for dark mode.
Adapting it
- Replace the
Future.delayedin_showTimedDialogwith your own future, closing the route in afinally. - Point
onRefreshat your reload and return its future.