Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static asset throughout the platform.
In this article, we will be looking into the implementation of Lottie animation in a flutter application. You can choose a wide range of Lottie files from here.
The following are some of the frequently used Lottie animation Properties:
- Animate: This property controls the motion of the animation.
- Reverse: This is used to as the name suggests, reverse the motion of the animation.
- Repeat: This is used to as the name suggests, repeat the animation multiple times.
Steps to implement lottie in flutter application
Step1: Adding the Lottie dependency.
Open the pubspec.yaml file and add the dependency as shown below:
dependencies:
lottie: ^3.3.1
Now run the below command in terminal.
flutter pub getStep 2 : Import the dependency
To import the dependency to the respective file you are going to use , but here main.dart file and use the below:
import 'package:lottie/lottie.dart';main.dart:
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LottiePage(),
);
}
}
Step 3 : Create LottiePage stateful class and Adding functionality
main.dart:
class LottiePage extends StatefulWidget {
@override
_LottiePageState createState() => _LottiePageState();
}
class _LottiePageState extends State<LottiePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Lottie.network(
'https://assets1.lottiefiles.com/private_files/lf30_QLsD8M.json',
height: 200.0,
repeat: true,
reverse: true,
animate: true,
),
Lottie.network(
'https://assets1.lottiefiles.com/private_files/lf30_yQtj4O.json',
repeat: true,
reverse: true,
animate: true,
),
],
),
),
);
}
}
To know more about column in flutter refer this article: Flutter – Row and Column Widgets
Explanation of the above Program :
Widget Structure
- LottiePage Class (Stateful Widget) : A stateful widget is used because Lottie animations might need to change or be interactive in future. It creates the state using createState(), which returns _LottiePageState.
- _LottiePageState Class : This class manages the state and builds the UI using the build() method.
- build() : It returns a Scaffold widget, which is the basic structure of a Flutter app containing an app bar and body.
- App Bar : Displays an AppBar with a title "GeeksForGeeks", a green background, and centers the title. The automaticallyImplyLeading: false disables the back button in the AppBar.
- Body : The body contains a Center widget with a Column, which stacks its children vertically.
Inside the Column, there are two Lottie animations fetched from the web using Lottie.network()
- Lottie.network() : Loads a Lottie animation from a URL.
- height: 200.0 : Sets the height of the first animation.
- repeat: true : The animation loops.
- reverse: true : The animation will reverse after playing forward.
- animate: true : The animation runs automatically.
The second Lottie widget has similar behavior but uses a different animation URL.
Complete Source code:
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
// Entry point of the application
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LottiePage(),
);
}
}
class LottiePage extends StatefulWidget {
@override
_LottiePageState createState() => _LottiePageState();
}
class _LottiePageState extends State<LottiePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeeksForGeeks"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// First Lottie animation
Lottie.network(
'https://assets1.lottiefiles.com/private_files/lf30_QLsD8M.json',
height: 200.0,
repeat: true,
reverse: true,
animate: true,
),
// Second Lottie animation
Lottie.network(
'https://assets1.lottiefiles.com/private_files/lf30_yQtj4O.json',
repeat: true,
reverse: true,
animate: true,
),
],
),
),
);
}
}