Flutter – Slide Transition Widget
Last Updated :
24 Apr, 2025
In this article, we will explore the Flutter Slide Transition Widget. The Slide Transition widget allows animating a widget’s position relative to its normal position. It helps us to develop a visually appealing and interactive user Interface. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Open your VS-code create a Flutter Application And Installing Module using the following steps:
- Open the Command Palette (Ctrl+Shift+P (Cmd+Shift+P on macOS)).
- Select the Flutter: New Project command and press Enter.
- Select Application and press Enter.
- Select a Project location.
- Enter your desired Project name.
Now your project structure looks like this:
Step 2: Create a SlideTransition widget by writing the following code inside your main.dart file
Dart
import 'package:flutter/material.dart' ;
void main() {
runApp( const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Slide Transition Widget' ,
home: SlideTransitionWidget(),
);
}
}
class SlideTransitionWidget extends StatefulWidget {
const SlideTransitionWidget({super.key});
@override
State<SlideTransitionWidget> createState() => _SlideTransitionWidgetState();
}
class _SlideTransitionWidgetState extends State<SlideTransitionWidget>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this ,
)..repeat(reverse: true );
late final Animation<Offset> _offsetAnimation = Tween<Offset>(
begin: Offset.zero,
end: const Offset(1.5, 0.0),
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.elasticIn,
));
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text( 'SlideTransition Sample' ),
backgroundColor: Colors.green),
body: Center(
child: SlideTransition(
position: _offsetAnimation,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.network(
),
)),
);
}
}
|
Now let’s understand the complete code:
import 'package:flutter/material.dart
We import flutter/material.dart package so that we can implement the material design, we can use widgets like stateless widgets, materialApp, etc.
void main() {
runApp(const MyApp());
}
Every flutter code has an entry point from the main function, inside the main function we call the runApp method provided by ‘flutter/material.dart’ package and we pass the widget MyApp as a parameter.
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Slide Transition Widget',
home: SlideTransitionWidget(),
);
}
}
Now we create our stateless widget MyApp by extending the stateless widget class provided by the material.dart package, then we override the build method implemented in statelessWidget class and return the materialApp widget with the required parameters.
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
)..repeat(reverse: true);
Initializing the Animation Controller. Animation Controller takes duration helps us to define the duration of the animation, “repeat(reverse: true)” means animation is repeating and in reverse too.
late final Animation<Offset> _offsetAnimation = Tween<Offset>(
begin: Offset.zero,
end: const Offset(1.5, 0.0),
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.elasticIn,
));
Here we define the offset of the animation using the Tween
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Disposing the controllers to releasing the resources
Scaffold(
appBar: AppBar(
title: const Text('SlideTransition Sample'),
backgroundColor: Colors.green),
body: Center(
child: SlideTransition(
position: _offsetAnimation,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.network(
'https://media.geeksforgeeks.org/wp-content/cdn-uploads/20210419113249/gfg-new-logo-min.png'),
),
)),
);
representing the app bar, setting the color of the AppBar, in the center defining the SlideTrasition widget, and inside that we load the geeksforgeeks logo.
Step 3: Run the following code by selecting the Android emulator or on your physical device by connecting it.
Output Screenshot:
Output Video:
Similar Reads
Flutter - SizeTransition Widget
In this article, we will explore the Flutter SizeTransition Widget. The Size Transition Widget is a key tool in Flutter that lets you animate the size of a child widget. It can be used to make a widget appear or vanish, create a zoom-in or zoom-out effect, and for many more things. A sample video is
7 min read
Flutter - Transform Widget
The Transform widget in Flutter is used to apply various transformations to its child widget, such as rotation, translation (positioning), scaling, and skewing. In this article, we are going to implement all transformations such as rotation, translation (positioning), scaling, and skewing made by th
6 min read
Flutter - ShaderMask Widget
ShaderMask is a widget in Flutter that applies a shader to its child. It allows you to create various visual effects by manipulating the colors and gradients of the child widget. In this article, we are going to implement the ShaderMask widget and explore some properties of it. Basic Syntax of Shade
4 min read
Flutter - FractionalTranslation Widget
In Flutter, the FractionalTranslation widget allows you to translate its child widget by a fraction of its own size along both the horizontal and vertical axes. In this article, we are going to implement the FractionalTranslation widget and explore some properties of it. A sample Image is given belo
3 min read
Flutter - SliverAppBar Widget
SliverAppBar is a Material Design widget in Flutter that gives a scrollable or collapsible app bar. The word Sliver is given to scrollable areas here. It allows us to create an app bar that can change appearance, blend in the background, or even disappear as we scroll. We already had AppBar widget i
5 min read
Flutter - Rotate Transition
In Flutter, the page_transition package is used to create beautiful page transitions. It provides a wide range of effects that can be used from moving from one route to another. It is very convenient to use. In this article, we will explore the same by building a simple application. Steps to Impleme
3 min read
Opacity Widget in Flutter
The Opacity widget that makes its child partially transparent. This class colors its child into an intermediate buffer and then merges the child back into the scene partially transparent. For values of opacity other than 0.0 and 1.0, this class is relatively expensive as it needs coloring the child
2 min read
RotatedBox Widget in Flutter
The RotatedBox widget is used to rotate its child by an integral number of quarter turns. It is used to orient its child widgets into either horizontal or vertical orientation. Furthermore, it is very lightweight and can be used for designing various UI as it gives flexibility to the user over the D
2 min read
Flutter - Stateful Widget
A Stateful Widget has states in it. To understand a Stateful Widget, you need to have a clear understanding of widgets and state management. A state can be defined as "an imperative changing of the user interface," and a widget is "an immutable description of the part of the user interface". To lear
4 min read
Flutter - NavigationRail Widget
NavigationRail is a Flutter widget that allows creating a navigation bar at the left or right location of the screen. Like the bottom navigation bar which sticks to the bottom, we have a navigation rail that stays on one of the sides. NavigationRail is suitable for large viewports like on desktop or
3 min read