Floating SnackBar in Flutter
Last Updated :
11 Apr, 2025
The floating_snackbar in Flutter is used to display a temporary message to users, often as feedback after an action.
We know how to show the snack bar in a flutter, but everybody wants the application must look more interactive and user-friendly, that's why we can use animations or new designs in the application, And also Floating snack bar is one of them. A snack bar is a pop-up that animates from the bottom of the application to show the user interaction or user activity.
How to Use:
TextButton(
onPressed: () {
floatingSnackBar(
message: 'Hi GeeksforGeeks, we are back',
context: context,
textColor: Colors.black,
textStyle: const TextStyle(color: Colors.green),
duration: const Duration(milliseconds: 4000),
backgroundColor: Color.fromARGB(255, 220, 234, 236),
);
},
child: const Text('Show SnackBar'),
);
Step By Step Implementation
Step 1: Create a new Flutter Application
Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.
flutter create app_name
To know more about it refer this article: Creating a Simple Application in Flutter.
Step 2: Adding the Dependency
To add the dependency to the pubspec.yaml file, add floating_snackbar as a dependency in the dependencies part of the pubspec.yaml file, as shown below:
Dart
dependencies:
flutter:
sdk: flutter
floating_snackbar: ^1.0.5
Now run the below command in the terminal.
flutter pub get
Or
Run the below command in the terminal.
flutter pub add floating_snackbar
Step 3: Import the package into the main file
Adding material package that gives us the important functions and calls the run app method in the main function that will call our application.
import 'package:flutter/material.dart';
import 'package:floating_snackbar/floating_snackbar.dart';
void main() {
runApp(RunMyApp());
}
Step 4: Creating a Stateless Widget
Now we have to make a stateless widget because our application does not go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more.
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp();
}
}
Step 5: Working with Scaffold.
Give the home property and there can be a scaffold widget that has the property of AppBar and body. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(
title: Text('Copy to clipboard'),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body:
),
Step 6: Using Floating SnackBar
Now the main part of the example, uses the center widget to show the button in the center of the body of the application, then in on pressed property we can use the Floating SnackBar and assign its parameters.
Complete Source Code:
Dart
// Import necessary packages
import 'package:floating_snackbar/floating_snackbar.dart';
import 'package:flutter/material.dart';
// Main function to run the app
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: RunMyApp(),
));
}
// Define a stateful widget
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
// Define the state for the stateful widget
class _RunMyAppState extends State<RunMyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Floating Snackbar'),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: TextButton(
onPressed: () {
// Display the floating snackbar
floatingSnackBar(
message: 'Hi GeeksforGeeks, we are back',
context: context,
textColor: Colors.black,
textStyle: const TextStyle(color: Colors.green),
duration: const Duration(milliseconds: 4000),
backgroundColor: Color.fromARGB(255, 220, 234, 236),
);
},
child: const Text('Show Floating SnackBar'),
),
),
);
}
}
Output:
Similar Reads
Flutter - Creating Snackbar Using GetX Library
Sometimes, it's very useful to show the message when a certain action takes place in our app. Let's suppose, we have a list of items and we want to delete any item from the list then after deleting those items, some message should be displayed to inform the user that the item has been deleted. If we
2 min read
Flutter - Creating App Intro Screens
Flutter is known for its easiness to create cross-platform applications. Either it is creating introduction screens for the app or any screen. We got an easy way to create Intros for the app using the intro_slider library of Flutter. In this article, we are going to implement it in the sample app.
4 min read
Shimmer Container in Flutter
A Shimmer Container is the fade-in and fade-out effect, We can show it instead using CircularProgressIndicator or Linear progress indicator that looks decent. While fetching the data from the API or from the database we need to wait as it is an asynchronous process, we need to do something on the us
3 min read
Flutter - Dots Indicator
Dots Indicator can be used to Show an increment or decrement to a value in a Flutter application through the UI. Moreover, it can also be used as an Increment or decrement component for a value through user interaction. To summarize its use case it can be improvised to use for multiple functionaliti
4 min read
Clipping in Flutter
In computer graphics, the act of restricting the rendering to a particular area is called Clipping. As developers, we use clipping to create awesome-looking, custom user interfaces with special effects. We have two types of Clipping that are ClipOval and ClipRRect. ClipOval A widget that clips its c
2 min read
Rive animations in Flutter
Rive is a very useful animation tool that can create beautiful animations and we can add these in our Application. In flutter, we can add animations by writing so many lines of code but this is not a good practice for a developer. Instead of writing lines of code to create animation, we can create o
2 min read
Flutter - Confetti Library
Want to add confetti on the screen in Flutter. Suppose if a user won a game or any reward, there should be some confetti blast on the screen. Was not an easy task before, but it is now with Flutter library - confetti. In this article, we will look at how to implement confetti in Flutter, then it can
4 min read
Routes and Navigator in Flutter
Route: Apps are the new trend. The number of apps available in the Play Store and App Store nowadays is quite a lot. The apps display their content in a full-screen container called pages or screens. In flutter, the pages or screens are called Routes. In android, these pages/screens are referred to
4 min read
Flow Widget in Flutter
The Flow widget in Flutter is a layout widget that positions children's elements in a flow along with sizing and positions its children proficiently using FlowDelegate. It allows you to create a grid-like layout where the children are positioned according to a given alignment, and they flow from one
4 min read
Gradient in Flutter Applications
Gradients are considered to be eye-catching elements in UI/UX and graphic design. Having gradient elements in your apps can really elate your app in terms of user experience and feel.In this tutorial, we will be looking at how we can implement gradients in flutter.Flutter offers 3 types of gradients
7 min read