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. Basically here, we are creating a new Flutter application using a command prompt.

- Delete the default code from the main.dart file and write your own code.
- Now, add fluttertoast in dependencies of the pubspec.yaml file:
Some Properties of showToast:
- msg: toast message.
- toastLength: Duration of toast
- backgroundColor: Background color to be shown.
- textColor: Text color to be shown.
- fontSize: Font size of toast message.
Example: main.dart
Dart
import 'package:flutter/material.dart' ;
import 'package:fluttertoast/fluttertoast.dart' ;
void main() {
runApp( const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Demo" ,
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false ,
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text( "GeeksforGeeks" ),
backgroundColor: Colors.green,
),
body: Center(
child: TextButton(
onPressed: () {
Fluttertoast.showToast(
msg: 'GeeksforGeeks' ,
backgroundColor: Colors.grey,
);
},
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 the design changes as follows:

If the font size is set to 25 and gravity is set to ToastGravity.TOP and text color is set to pink the design changes as follows:

If you are Not Comfortable using Dependency then just use the below snippet:
eg:
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(“My amazing message! O.o”)));
Customize SnackBar for your own.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Sep, 2022
Like Article
Save Article