Skip to content

Instantly share code, notes, and snippets.

@obumnwabude
Last active July 26, 2022 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obumnwabude/368c3ebd8212e818fb28bb1bb2c67063 to your computer and use it in GitHub Desktop.
Save obumnwabude/368c3ebd8212e818fb28bb1bb2c67063 to your computer and use it in GitHub Desktop.
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