How to create a shimmer effect in flutter

Introduction

When data is being loaded from the server or database, there is a little animation called shimmer that makes this known. Numerous apps, such as social networking, online shopping, and other platforms, use this shimmer effect. The shimmer is utilized as an alternate effect that provides a more aesthetically pleasing user interface (UI) for the user instead of progress bars and loaders. It is possible to modify the shimmer effect to match the user interface.

This article describes the fundamental implementation of the shimmer effect in flutter apps.

When retrieving data from data source, which could be local or remote, shimmer effects are employed as loading indicators. 
When the data is accessible, it depicts view that may resemble the actual data that will be displayed on the screen.

Adding a shimmer effect

Let’s begin by making a new Flutter project.

flutter create shimmer_app

Include the shimmer dependency in the pubspec.yaml file, then execute pub get to install it.

dependencies:
  flutter:
    sdk: flutter
 shimmer: ^2.0.0

Add the following code to the main.dart file

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Shimmer',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: ShimmerEffectPage(),
    );
  }
}
class ShimmerPage extends StatefulWidget {
  @override
  _ShimmerPageState createState() => _ShimmerPageState();
}

class _ShimmerPageState extends State<ShimmerPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Shimmer'),
      ),
      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[200],
                highlightColor: Colors.grey[100],
                direction: ShimmerDirection.ltr,
                child: ListView.builder(
                  itemBuilder: (_, __) => Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          width: 40.0,
                          height: 40.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,
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  itemCount: 8,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories