### Setup OKToast with MaterialApp Builder Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Use the MaterialApp builder property to wrap the application, which can help resolve MediaQuery-related errors in specific configurations. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'My App', home: const HomePage(), builder: (BuildContext context, Widget? child) { return OKToast( backgroundColor: Colors.grey, textStyle: const TextStyle(fontSize: 19.0, color: Colors.white), animationCurve: Curves.easeIn, animationDuration: const Duration(milliseconds: 200), duration: const Duration(seconds: 3), child: child!, ); }, ); } } ``` -------------------------------- ### Initialize and Use OKToast in Flutter Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Wraps the root MaterialApp with OKToast and demonstrates using showToast and showToastWidget to display notifications. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; // 1. import library void main() => runApp( MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return OKToast( // 2. wrap your app with OKToast child: MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key}) : super(key: key); @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; void _incrementCounter() { _counter++; // 3.1 use showToast method showToast( "$_counter", duration: Duration(seconds: 2), position: ToastPosition.bottom, backgroundColor: Colors.black.withOpacity(0.8), radius: 13.0, textStyle: TextStyle(fontSize: 18.0), ); showToast( "$_counter", duration: Duration(seconds: 2), position: ToastPosition.top, backgroundColor: Colors.black.withOpacity(0.8), radius: 3.0, textStyle: TextStyle(fontSize: 30.0), ); // 3.2 use showToastWidget method to custom widget Widget widget = Center( child: ClipRRect( borderRadius: BorderRadius.circular(30.0), child: Container( width: 40.0, height: 40.0, color: Colors.grey.withOpacity(0.3), child: Icon( Icons.add, size: 30.0, color: Colors.green, ), ), ), ); ToastFuture toastFuture = showToastWidget( widget, duration: Duration(seconds: 3), onDismiss: () { print("the toast dismiss"); // the method will be called on toast dismiss. }, ); // can use future Future.delayed(Duration(seconds: 1), () { toastFuture.dismiss(); // dismiss }); setState(() { }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("ktoast demo"), ), body: Stack( children: [ Center( child: ListView( children: [ Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), Padding( padding: const EdgeInsets.all(8.0), child: RaisedButton( onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (ctx) => MyHomePage())); }, ), ), Padding( padding: const EdgeInsets.all(8.0), child: RaisedButton( onPressed: _incrementCounter, child: Text('toast'), ), ), ], ), ), ], ), ); } } ``` -------------------------------- ### Configure OKToast Widget Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Wrap the application root with the OKToast widget to define global toast styling, animation parameters, and behavior settings. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return OKToast( // Default toast styling textStyle: const TextStyle(fontSize: 16.0, color: Colors.white), backgroundColor: Colors.black.withOpacity(0.8), radius: 10.0, position: ToastPosition.center, // Animation configuration animationBuilder: const OpacityAnimationBuilder().call, animationDuration: const Duration(milliseconds: 250), animationCurve: Curves.easeIn, // Toast behavior duration: const Duration(seconds: 3), dismissOtherOnShow: false, movingOnWindowChange: true, handleTouch: false, // Text configuration textDirection: TextDirection.ltr, textPadding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0), textAlign: TextAlign.center, child: MaterialApp( title: 'My App', home: const HomePage(), ), ); } } ``` -------------------------------- ### OKToast Configuration Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Configuration parameters for the OKToast wrapper, typically used at the root of the application. ```APIDOC ## OKToast Configuration ### Description Defines the global style and behavior for toasts within the application. ### Parameters #### Request Body - **child** (Widget) - Required - Usually Material App - **textStyle** (TextStyle) - Optional - **radius** (double) - Optional - **backgroundColor** (Color) - Optional - **position** (ToastPosition) - Optional - **dismissOtherOnShow** (bool) - Optional - If true, other toasts will be dismissed. Default false. - **movingOnWindowChange** (bool) - Optional - If true, when the size changes, toast is moved. Default true. - **textDirection** (TextDirection) - Optional - **textPadding** (EdgeInsetsGeometry) - Optional - Outer margin of text - **textAlign** (TextAlign) - Optional - When the text wraps, the align of the text. - **handleTouch** (bool) - Optional - Default is false, if it's true, can responed use touch event. - **animationBuilder** (OKToastAnimationBuilder) - Optional - Add animation to show / hide toast. - **animationDuration** (Duration) - Optional - The duration of animation. - **animationCurve** (Curve) - Optional - Curve of animation. - **duration** (Duration) - Optional - Default duration of toast. ``` -------------------------------- ### Manage Toast Lifecycle with ToastFuture Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Demonstrates how to show persistent toasts, dismiss them with or without animations, and check their current status. ```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; class ToastFutureExamples extends StatefulWidget { const ToastFutureExamples({super.key}); @override State createState() => _ToastFutureExamplesState(); } class _ToastFutureExamplesState extends State { ToastFuture? _persistentToast; void showPersistentToast() { // Show a toast that persists until manually dismissed (duration: Duration.zero) _persistentToast = showToastWidget( Center( child: Container( padding: const EdgeInsets.all(20.0), decoration: BoxDecoration( color: Colors.black87, borderRadius: BorderRadius.circular(12.0), ), child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text( 'Persistent Toast', style: TextStyle(color: Colors.white, fontSize: 18.0), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: () => _persistentToast?.dismiss(), child: const Text('Tap to dismiss'), ), ], ), ), ), duration: Duration.zero, // Toast will not auto-dismiss handleTouch: true, // Enable touch events on the toast ); } void dismissWithAnimation() { final ToastFuture toast = showToast( 'This toast will be dismissed with animation', duration: const Duration(seconds: 10), ); Future.delayed(const Duration(seconds: 2), () { // Dismiss with fade-out animation toast.dismiss(showAnim: true); }); } void dismissWithoutAnimation() { final ToastFuture toast = showToast( 'This toast will be dismissed instantly', duration: const Duration(seconds: 10), ); Future.delayed(const Duration(seconds: 2), () { // Dismiss instantly without animation toast.dismiss(showAnim: false); }); } void checkToastStatus() { final ToastFuture toast = showToast('Check my status'); // Check if toast is mounted (visible) debugPrint('Is mounted: ${toast.mounted}'); // Check if toast has been dismissed debugPrint('Is dismissed: ${toast.dismissed}'); } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton( onPressed: showPersistentToast, child: const Text('Show Persistent Toast'), ), ElevatedButton( onPressed: dismissWithAnimation, child: const Text('Dismiss With Animation'), ), ], ); } } ``` -------------------------------- ### Show Custom Widget Toast with Icon Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Displays a custom widget with an icon as a toast. Requires importing 'package:oktoast/oktoast.dart'. ```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; class CustomWidgetToastExamples extends StatelessWidget { const CustomWidgetToastExamples({super.key}); void showIconToast() { // Custom widget toast with icon final Widget toastWidget = Center( child: ClipRRect( borderRadius: BorderRadius.circular(30.0), child: Container( width: 60.0, height: 60.0, color: Colors.green.withOpacity(0.9), child: const Icon( Icons.check, size: 40.0, color: Colors.white, ), ), ), ); showToastWidget( toastWidget, duration: const Duration(seconds: 2), position: ToastPosition.center, ); } void showLoadingToast() { // Loading indicator toast final Widget loadingWidget = Container( padding: const EdgeInsets.all(20.0), decoration: BoxDecoration( color: Colors.black.withOpacity(0.8), borderRadius: BorderRadius.circular(10.0), ), child: const Column( mainAxisSize: MainAxisSize.min, children: [ CircularProgressIndicator(color: Colors.white), SizedBox(height: 12.0), Text( 'Loading...', style: TextStyle(color: Colors.white, fontSize: 16.0), ), ], ), ); showToastWidget( loadingWidget, duration: const Duration(seconds: 3), position: ToastPosition.center, handleTouch: false, ); } void showComplexToast() { // Complex toast with multiple elements final Widget complexWidget = Container( margin: const EdgeInsets.symmetric(horizontal: 20.0), padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12.0), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.2), blurRadius: 10.0, offset: const Offset(0, 4), ), ], ), child: Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.info_outline, color: Colors.blue, size: 28.0), const SizedBox(width: 12.0), const Flexible( child: Text( 'New message received from John', style: TextStyle(fontSize: 16.0, color: Colors.black87), ), ), ], ), ); showToastWidget( complexWidget, duration: const Duration(seconds: 4), position: ToastPosition.top, onDismiss: () { debugPrint('Complex toast dismissed'); }, ); } void showToastAndDismissEarly() { // Show toast and dismiss it programmatically before duration expires final ToastFuture toastFuture = showToastWidget( Container( padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.indigo, borderRadius: BorderRadius.circular(8.0), ), child: const Text( 'This will be dismissed in 1 second', style: TextStyle(color: Colors.white), ), ), duration: const Duration(seconds: 5), onDismiss: () { debugPrint('Toast was dismissed early'); }, ); // Dismiss after 1 second instead of waiting for 5 seconds Future.delayed(const Duration(seconds: 1), () { toastFuture.dismiss(); }); } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton( onPressed: showIconToast, child: const Text('Show Icon Toast'), ), ElevatedButton( onPressed: showLoadingToast, child: const Text('Show Loading Toast'), ), ElevatedButton( onPressed: showComplexToast, child: const Text('Show Complex Toast'), ), ], ); } } ``` -------------------------------- ### Alternative OKToast Wrapper for No MediaQuery Error Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md If you encounter a 'No MediaQuery widget found' error, use this alternative builder pattern to include OKToast within your MaterialApp. ```dart MaterialApp( builder: (BuildContext context, Widget? widget) { return OKToast(child: widget); }, ); ``` -------------------------------- ### Show Toast at Different Positions Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Demonstrates how to display toasts at the top, center, and bottom of the screen using the 'position' parameter. Each toast can have a distinct background color. ```dart // Toast at top of screen showToast( 'Top notification', position: ToastPosition.top, backgroundColor: Colors.blue.withOpacity(0.8), ); // Toast at center of screen (default) showToast( 'Center notification', position: ToastPosition.center, backgroundColor: Colors.orange.withOpacity(0.8), ); // Toast at bottom of screen showToast( 'Bottom notification', position: ToastPosition.bottom, backgroundColor: Colors.purple.withOpacity(0.8), ); ``` -------------------------------- ### showToast Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Displays a text-based toast message. ```APIDOC ## showToast ### Description Displays a simple text message as a toast. ### Parameters #### Request Body - **msg** (String) - Required - Text of toast. - **context** (BuildContext) - Optional - **duration** (Duration) - Optional - **position** (ToastPosition) - Optional - **textStyle** (TextStyle) - Optional - **textPadding** (EdgeInsetsGeometry) - Optional - **backgroundColor** (Color) - Optional - **radius** (double) - Optional - **onDismiss** (Function) - Optional - **textDirection** (TextDirection) - Optional - **dismissOtherToast** (bool) - Optional - **textAlign** (TextAlign) - Optional - **animationBuilder** (OKToastAnimationBuilder) - Optional - **animationDuration** (Duration) - Optional - **animationCurve** (Curve) - Optional ``` -------------------------------- ### Import OKToast Library Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Import the oktoast library into your Dart file to use its functionalities. ```dart import 'package:oktoast/oktoast.dart'; ``` -------------------------------- ### showToastWidget Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Displays a custom widget as a toast. ```APIDOC ## showToastWidget ### Description Displays a custom widget instead of a simple text message. ### Parameters #### Request Body - **widget** (Widget) - Required - The widget you want to display. - **context** (BuildContext) - Optional - **duration** (Duration) - Optional - **position** (ToastPosition) - Optional - **onDismiss** (Function) - Optional - **dismissOtherToast** (bool) - Optional - **textDirection** (TextDirection) - Optional - **handleTouch** (bool) - Optional - **animationBuilder** (OKToastAnimationBuilder) - Optional - **animationDuration** (Duration) - Optional - **animationCurve** (Curve) - Optional ``` -------------------------------- ### Show Simple Toast Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Call the showToast method with the desired content string to display a basic toast message. ```dart showToast('content'); ``` -------------------------------- ### Show Toast with Custom Position Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Illustrates how to position a toast at a custom location using 'ToastPosition.center.copyWith'. This allows for alignment adjustments and offset from the center. ```dart // Custom position using copyWith final customPosition = ToastPosition.center.copyWith( align: Alignment.centerLeft, offset: 20.0, ); showToast( 'Custom aligned toast', position: customPosition, ); ``` -------------------------------- ### Show Loading Indicator Toast Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Displays a toast with a loading indicator and text. The `handleTouch` parameter is set to `false` to prevent user interaction while the toast is visible. ```dart final Widget loadingWidget = Container( padding: const EdgeInsets.all(20.0), decoration: BoxDecoration( color: Colors.black.withOpacity(0.8), borderRadius: BorderRadius.circular(10.0), ), child: const Column( mainAxisSize: MainAxisSize.min, children: [ CircularProgressIndicator(color: Colors.white), SizedBox(height: 12.0), Text( 'Loading...', style: TextStyle(color: Colors.white, fontSize: 16.0), ), ], ), ); showToastWidget( loadingWidget, duration: const Duration(seconds: 3), position: ToastPosition.center, handleTouch: false, ); ``` -------------------------------- ### Show Basic Toast Notification Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Displays a simple text toast with default styling. Ensure the 'oktoast' package is imported. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; class ToastExamples extends StatelessWidget { const ToastExamples({super.key}); void showBasicToast() { // Simple text toast with default styling showToast('Hello, World!'); } // ... other methods @override Widget build(BuildContext context) { return ElevatedButton( onPressed: showCustomizedToast, child: const Text('Show Toast'), ); } } ``` -------------------------------- ### Implement Custom Animation Builders Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Define custom show/hide transitions by creating functions or extending BaseAnimationBuilder. Pass the builder's call method to the animationBuilder parameter in showToast. ```dart import 'dart:math' show min; import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; // Custom animation builder using function syntax Widget slideUpAnimation( BuildContext context, Widget child, AnimationController controller, double percent, ) { return Transform.translate( offset: Offset(0, 50 * (1 - percent)), child: Opacity(opacity: percent, child: child), ); } // Custom animation builder extending BaseAnimationBuilder class ScaleAnimationBuilder extends BaseAnimationBuilder { const ScaleAnimationBuilder(); @override Widget buildWidget( BuildContext context, Widget child, AnimationController controller, double percent, ) { return Transform.scale( scale: percent, child: Opacity(opacity: percent, child: child), ); } } // MIUI 10-style animation (slide up with opacity) class Miui10AnimBuilder extends BaseAnimationBuilder { const Miui10AnimBuilder(); @override Widget buildWidget( BuildContext context, Widget child, AnimationController controller, double percent, ) { final double opacity = min(1.0, percent + 0.2); final double offset = (1 - percent) * 20; return Opacity( opacity: opacity, child: Transform.translate(offset: Offset(0, offset), child: child), ); } } class AnimationExamples extends StatelessWidget { const AnimationExamples({super.key}); void showWithBuiltInAnimations() { // Using built-in OpacityAnimationBuilder (default) showToast( 'Opacity Animation', animationBuilder: const OpacityAnimationBuilder().call, animationDuration: const Duration(milliseconds: 300), ); // Using built-in OffsetAnimationBuilder (slide from bottom) showToast( 'Offset Animation', animationBuilder: const OffsetAnimationBuilder( maxOffsetX: 0, maxOffsetY: 100, ).call, animationDuration: const Duration(milliseconds: 400), position: ToastPosition.bottom, ); } void showWithCustomAnimations() { // Using function-based custom animation showToast( 'Slide Up Animation', animationBuilder: slideUpAnimation, animationDuration: const Duration(milliseconds: 350), ); // Using class-based custom animation showToast( 'Scale Animation', animationBuilder: const ScaleAnimationBuilder().call, animationDuration: const Duration(milliseconds: 300), ); // Using MIUI-style animation showToast( 'MIUI 10 Style', animationBuilder: const Miui10AnimBuilder().call, animationDuration: const Duration(milliseconds: 200), ); } void configureGlobalAnimation() { // Set animation globally in OKToast widget configuration // See OKToast Widget Configuration section } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton( onPressed: showWithBuiltInAnimations, child: const Text('Built-in Animations'), ), ElevatedButton( onPressed: showWithCustomAnimations, child: const Text('Custom Animations'), ), ], ); } } ``` -------------------------------- ### Add OKToast Dependency Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Add the oktoast package to your project's pubspec.yaml file using the flutter pub add command. ```shell flutter pub add oktoast ``` -------------------------------- ### Wrap App Widget with OKToast Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Wrap your main app widget (e.g., MaterialApp) with OKToast to enable toast display. This ensures toasts appear above all other controls and allows context caching. ```dart OKToast( /// set toast style, optional child:MaterialApp() ); ``` -------------------------------- ### Implement OKToast in a Flutter Application Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Wraps the MaterialApp with the OKToast widget to provide global toast configuration and functionality. Requires the oktoast package to be added to the project dependencies. ```dart import 'dart:async'; import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return OKToast( textStyle: const TextStyle(fontSize: 16.0, color: Colors.white), backgroundColor: Colors.black.withOpacity(0.8), radius: 10.0, position: ToastPosition.center, duration: const Duration(seconds: 3), animationDuration: const Duration(milliseconds: 250), animationCurve: Curves.easeIn, dismissOtherOnShow: false, child: MaterialApp( title: 'OKToast Complete Example', theme: ThemeData(primarySwatch: Colors.blue), home: const ToastDemoPage(), ), ); } } class ToastDemoPage extends StatefulWidget { const ToastDemoPage({super.key}); @override State createState() => _ToastDemoPageState(); } class _ToastDemoPageState extends State { ToastFuture? _currentToast; void _showSimpleToast() { showToast('Hello from OKToast!'); } void _showStyledToast() { showToast( 'Styled Toast Message', position: ToastPosition.bottom, backgroundColor: Colors.green, radius: 20.0, textStyle: const TextStyle( fontSize: 18.0, color: Colors.white, fontWeight: FontWeight.bold, ), ); } void _showSuccessWidget() { showToastWidget( Container( padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0), decoration: BoxDecoration( color: Colors.green, borderRadius: BorderRadius.circular(25.0), ), child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.check_circle, color: Colors.white), SizedBox(width: 8.0), Text('Success!', style: TextStyle(color: Colors.white, fontSize: 16.0)), ], ), ), position: ToastPosition.center, duration: const Duration(seconds: 2), ); } void _showInteractiveToast() { _currentToast = showToastWidget( Container( margin: const EdgeInsets.all(20.0), padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12.0), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.15), blurRadius: 10.0, ), ], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text( 'Interactive Toast', style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: () => _currentToast?.dismiss(), child: const Text('Dismiss'), ), ], ), ), duration: Duration.zero, handleTouch: true, position: ToastPosition.center, ); } void _dismissAll() { dismissAllToast(showAnim: true); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('OKToast Demo')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: _showSimpleToast, child: const Text('Simple Toast'), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: _showStyledToast, child: const Text('Styled Toast'), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: _showSuccessWidget, child: const Text('Success Widget'), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: _showInteractiveToast, child: const Text('Interactive Toast'), ), const SizedBox(height: 12.0), ElevatedButton( onPressed: _dismissAll, style: ElevatedButton.styleFrom(backgroundColor: Colors.red), child: const Text('Dismiss All', style: TextStyle(color: Colors.white)), ), ], ), ), ); } } ``` -------------------------------- ### Configure ToastPosition for alignment and offset Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Use predefined positions or instantiate ToastPosition with custom Alignment and offset values. The copyWith method allows for incremental adjustments to existing positions. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; class ToastPositionExamples extends StatelessWidget { const ToastPositionExamples({super.key}); void showPredefinedPositions() { // Predefined position: center of screen showToast('Center', position: ToastPosition.center); // Predefined position: top of screen (offset: 75.0 from top) showToast('Top', position: ToastPosition.top); // Predefined position: bottom of screen (offset: -30.0 from bottom) showToast('Bottom', position: ToastPosition.bottom); } void showCustomPositions() { // Custom position: top-left corner const topLeftPosition = ToastPosition( align: Alignment.topLeft, offset: 100.0, ); showToast('Top Left', position: topLeftPosition); // Custom position: bottom-right with custom offset const bottomRightPosition = ToastPosition( align: Alignment.bottomRight, offset: -50.0, ); showToast('Bottom Right', position: bottomRightPosition); // Using copyWith to modify existing position final customCenter = ToastPosition.center.copyWith( offset: -100.0, // Move 100 pixels up from center ); showToast('Above Center', position: customCenter); } @override Widget build(BuildContext context) { return ElevatedButton( onPressed: showCustomPositions, child: const Text('Show Custom Positions'), ); } } ``` -------------------------------- ### Dismiss All Toasts Simultaneously Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Shows how to clear all currently visible toasts at once, with options for immediate removal or removal via fade-out animation. ```dart import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; class DismissAllExample extends StatelessWidget { const DismissAllExample({super.key}); void showMultipleToasts() { // Show several toasts at different positions showToast( 'Toast 1 - Top', position: ToastPosition.top, duration: const Duration(seconds: 10), ); showToast( 'Toast 2 - Center', position: ToastPosition.center, duration: const Duration(seconds: 10), ); showToast( 'Toast 3 - Bottom', position: ToastPosition.bottom, duration: const Duration(seconds: 10), ); } void clearAllToasts() { // Dismiss all toasts instantly dismissAllToast(); } void clearAllToastsWithAnimation() { // Dismiss all toasts with fade-out animation dismissAllToast(showAnim: true); } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton( onPressed: showMultipleToasts, child: const Text('Show Multiple Toasts'), ), ElevatedButton( onPressed: clearAllToasts, child: const Text('Dismiss All Toasts'), ), ElevatedButton( onPressed: clearAllToastsWithAnimation, child: const Text('Dismiss All With Animation'), ), ], ); } } ``` -------------------------------- ### Show Custom Toast Widget Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Use showToastWidget to display a custom widget as the toast content. Position and duration are optional parameters with default values. ```dart showToastWidget(Text('hello oktoast')); ``` -------------------------------- ### Show Complex Toast with Info Icon and Text Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Displays a toast with multiple UI elements, including an info icon, text, and a shadow effect. The `onDismiss` callback is used to log a message when the toast is dismissed. ```dart final Widget complexWidget = Container( margin: const EdgeInsets.symmetric(horizontal: 20.0), padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12.0), boxShadow: [ BoxShadow( color: Colors.black.withOpacity(0.2), blurRadius: 10.0, offset: const Offset(0, 4), ), ], ), child: Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.info_outline, color: Colors.blue, size: 28.0), const SizedBox(width: 12.0), const Flexible( child: Text( 'New message received from John', style: TextStyle(fontSize: 16.0, color: Colors.black87), ), ), ], ), ); showToastWidget( complexWidget, duration: const Duration(seconds: 4), position: ToastPosition.top, onDismiss: () { debugPrint('Complex toast dismissed'); }, ); ``` -------------------------------- ### Programmatically Dismiss Toast Early Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Shows a toast and then dismisses it programmatically after a short delay, overriding the initially set duration. This demonstrates control over the `ToastFuture` returned by `showToastWidget`. ```dart final ToastFuture toastFuture = showToastWidget( Container( padding: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: Colors.indigo, borderRadius: BorderRadius.circular(8.0), ), child: const Text( 'This will be dismissed in 1 second', style: TextStyle(color: Colors.white), ), ), duration: const Duration(seconds: 5), onDismiss: () { debugPrint('Toast was dismissed early'); }, ); // Dismiss after 1 second instead of waiting for 5 seconds Future.delayed(const Duration(seconds: 1), () { toastFuture.dismiss(); }); ``` -------------------------------- ### Show Customized Toast Notification Source: https://context7.com/openflutter/flutter_oktoast/llms.txt Displays a fully customized text toast with options for duration, position, background color, text style, padding, and animations. The 'onDismiss' callback can be used to perform actions when the toast disappears. ```dart showToast( 'Operation completed successfully!', duration: const Duration(seconds: 2), position: ToastPosition.bottom, backgroundColor: Colors.green.withOpacity(0.9), radius: 20.0, textStyle: const TextStyle( fontSize: 18.0, color: Colors.white, fontWeight: FontWeight.w500, ), textPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0), textAlign: TextAlign.center, textDirection: TextDirection.ltr, dismissOtherToast: true, animationDuration: const Duration(milliseconds: 300), animationCurve: Curves.easeInOut, onDismiss: () { debugPrint('Toast was dismissed'); }, ); ``` -------------------------------- ### dismissAllToast Source: https://github.com/openflutter/flutter_oktoast/blob/main/README.md Dismisses all currently visible toasts. ```APIDOC ## dismissAllToast ### Description Immediately dismisses all active toast notifications. ``` -------------------------------- ### Register Service Worker in Flutter Web Source: https://github.com/openflutter/flutter_oktoast/blob/main/example/web/index.html Use this snippet to register the Flutter service worker during the window load event. Ensure the service worker file path matches your project structure. ```javascript if ('serviceWorker' in navigator) { window.addEventListener('load', function () { navigator.serviceWorker.register('/flutter_service_worker.js'); }); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.