Can I create something similar to Toasts in Flutter?
Just a tiny notification window that is not directly in the face of the user and does not lock or fade the view behind it.
To show toast using snackbar: Step 1: Add the button (ElevatedButton) if you haven't already added it. Step 2: Inside the onPressed method of a button, create a SnackBar. Step 3: Just in the next line, add the ScaffoldMessenger, call the showSnackBar method and pass the SnackBar created previously.
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.
UPDATE: Scaffold.of(context).showSnackBar
is deprecated in Flutter 2.0.0 (stable)
You can access the parent ScaffoldMessengerState
using ScaffoldMessenger.of(context)
.
Then do something like
ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text("Sending Message"), ));
Snackbars are the official "Toast" from material design. See Snackbars.
Here is a fully working example:
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return const MaterialApp( home: Home(), ); } } class Home extends StatelessWidget { const Home({ Key key, }) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Snack bar'), ), body: Center( child: RaisedButton( onPressed: () => _showToast(context), child: const Text('Show toast'), ), ), ); } void _showToast(BuildContext context) { final scaffold = ScaffoldMessenger.of(context); scaffold.showSnackBar( SnackBar( content: const Text('Added to favorite'), action: SnackBarAction(label: 'UNDO', onPressed: scaffold.hideCurrentSnackBar), ), ); } }
Use fluttertoast plugin
Fluttertoast.showToast( msg: "This is a Toast message", // message toastLength: Toast.LENGTH_SHORT, // length gravity: ToastGravity.CENTER, // location timeInSecForIos: 1 // duration );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With