Skip to content

Instantly share code, notes, and snippets.

@obumnwabude
Last active Jul 26, 2022
Embed
What would you like to do?
Example usage of FlutterToast package
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(home: ExampleToast());
}
}
class ExampleToast extends StatefulWidget {
const ExampleToast({Key? key}) : super(key: key);
@override
State<ExampleToast> createState() => ExampleToastState();
}
class ExampleToastState extends State<ExampleToast> {
late final FToast fToast;
final Widget toast = Container(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.greenAccent,
),
child: const Text('Successful!'),
);
@override
void initState() {
super.initState();
fToast = FToast();
fToast.init(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Example Toast')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
child: const Text('Show Toast'),
onPressed: () => fToast.showToast(child: toast),
),
const SizedBox(height: 32),
ElevatedButton(
child: const Text('Show Customized Toast'),
onPressed: () => fToast.showToast(
child: toast,
fadeDuration: 3000,
gravity: ToastGravity.TOP,
toastDuration: Duration(seconds: 3),
),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment