Flutter: create a shimmer effect loader like the facebook list loader effect
Shimmer effects are loading indicators used when fetching data from a data source that can either be local or remote. It paints a view that may be similar to the actual data to be rendered on the screen when the data is available.
Instead of the usual CircularProgressIndicator or LinearProgressIndicator, shimmer effects present a more aesthetically pleasing view to the user and in some cases helps build up some anticipation of the data before it’s rendered on the screen.
Start by creating a new Flutter project in either of VS Code or Android Studio. Replace the Flutter default counter application in main.dart with your own stateful widget.
Implementing a shimmer effect
Let’s start by creating a new Flutter project.
flutter create shimmer loadingand add the dependencies and dev dependencies run command:
flutter pub add shimmer
Then open main.dart and add the following code like this:
import 'package:flutter/material.dart';
import 'package:shimmerloading/home/home.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}Before Create Home stateful widget create home.dart file in home folder, then inside of it with the following code:
import 'package:flutter/material.dart';
import 'package:shimmerloading/loader/loader.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
backgroundColor: Colors.pinkAccent,
),
body: Column(
children: [
Center(
child: ElevatedButton(
onPressed: ()=>{Navigator.push(context,MaterialPageRoute(builder: (context) => Loader()))},
child: Text("Loder")
),
)
],
),
);
}
}Before Create Loader stateful widget create Loader.dart file in home folder, then inside of it with the following code:
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';
class Loader extends StatefulWidget {
const Loader({Key? key}) : super(key: key);
@override
State<Loader> createState() => _LoaderState();
}
class _LoaderState extends State<Loader> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Loader"),
backgroundColor: Colors.pinkAccent,
),
body: Container(
width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget> [
Expanded(
child: Shimmer.fromColors(
baseColor: Colors.grey[300]!,
highlightColor: Colors.grey[100]!,
enabled: true,
child: ListView.builder(
itemBuilder: (_, __) => Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 48.0,
height: 48.0,
color: Colors.white,
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 8.0),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: double.infinity,
height: 8.0,
color: Colors.white,
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 2.0),
),
Container(
width: double.infinity,
height: 8.0,
color: Colors.white,
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 2.0),
),
Container(
width: 40.0,
height: 8.0,
color: Colors.white,
),
],
),
)
],
),
),
itemCount: 20,
),
),
),
],
),
),
);
}
}Output: