Fluttertoast is used to create a toast message by writing only one line of code. Below are some steps to create a Fluttertoast in Flutter. We can use the flutter_toast package to achieve a toast in Flutter. Let's see what we are going to build with a demo video.
Demo Video:
Constructor of showToast
Future<bool?> showToast({
required String msg,
Toast? toastLength,
int timeInSecForIosWeb = 1,
double? fontSize,
String? fontAsset,
ToastGravity? gravity,
Color? backgroundColor,
Color? textColor,
bool webShowClose = false,
dynamic webBgColor = "linear-gradient(to right, #00b09b, #96c93d)",
dynamic webPosition = "right",
})
Key Properties of showToast
Property | Description |
---|
msg | Toast message |
---|
toastLength | Duration of toast |
---|
backgroundColor | Background color of the toast |
---|
textColor | Text color of the toast |
---|
fontSize | Font size of the toast message |
---|
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 following 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 flutter_toast as a dependency in the dependencies part of the pubspec.yaml file, as shown below:
Dart
dependencies:
flutter:
sdk: flutter
fluttertoast: ^8.2.12
Now, run the below command in the terminal.
flutter pub get
Or
Run the below command in the terminal.
flutter pub add fluttertoast
Step 3: Import dependencies
To use libraries, import all of them in the respective .dart file.
import 'package:fluttertoast/fluttertoast.dart';
Step 4: Working with the main
Add the boilerplate code below in main.dart and create a basic structure with an MaterialApp.
Dart
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
// Entry point of the Flutter application
void main() {
runApp(const MyApp());
}
// Root widget of the application
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// Builds the MaterialApp widget
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Demo",
debugShowCheckedModeBanner: false,
home: const MyHomePage(),
);
}
}
// Stateful widget for the home page
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
// State class for MyHomePage
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("GeeksforGeeks"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: // Code Here
);
}
}
Step 5: Toast Method
Dart
// Displays a toast message when the button is pressed
Fluttertoast.showToast(
msg: 'GeeksforGeeks', // Message to display in the toast
backgroundColor: Colors.grey, // Background color of the toast
);
Complete Source Code
main.dart:
Dart
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
// Entry point of the Flutter application
void main() {
runApp(const MyApp());
}
// Root widget of the application
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// Builds the MaterialApp widget
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Demo",
debugShowCheckedModeBanner: false,
home: const MyHomePage(),
);
}
}
// Stateful widget for the home page
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
// State class for MyHomePage
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("GeeksforGeeks"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: TextButton(
onPressed: () {
// Displays a toast message when the button is pressed
Fluttertoast.showToast(
msg: 'GeeksforGeeks', // Message to display in the toast
backgroundColor: Colors.grey, // Background color of the toast
);
},
child: Container(
padding: const EdgeInsets.all(14),
color: Colors.green,
child: const Text(
'Show Toast',
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
}
Output:
If the font size is set to 25, the design changes as follows:
If the font size is set to 25 and gravity is set to ToastGravity.TOP design changes are as follows:
If the font size is set to 25 and gravity is set to ToastGravity.TOP and text color is set to blue, the design changes as follows:
If you are Not Comfortable using Dependency,, then just use the below snippet:
C++
ScaffoldMessenger.of(context,).
showSnackBar(SnackBar(content: Text("My amazing message! O.o")));
Customize SnackBar for your own.
Output:
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Flutter Tutorial This Flutter Tutorial is specifically designed for beginners and experienced professionals. It covers both the basics and advanced concepts of the Flutter framework.Flutter is Googleâs mobile SDK that builds native Android and iOS apps from a single codebase. It was developed in December 2017. When
7 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Dart Tutorial Dart is an open-source general-purpose programming language developed by Google. It supports application development on both the client and server side. However, it is widely used for the development of Android apps, iOS apps, IoT(Internet of Things), and web applications using the Flutter Framework
7 min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read