@@ -3,6 +3,14 @@ import 'dart:async';
3
3
import 'package:flutter/material.dart' ;
4
4
import 'package:flutter/services.dart' ;
5
5
6
+ /// Signature for a function that defines custom position mapping for a toast
7
+ ///
8
+ /// [child] is the toast widget to be positioned.
9
+ /// [gravity] is the gravity option for the toast which can be used to determine the position.
10
+ /// The function should return a [Widget] that defines the position of the toast.
11
+ /// If the position is not handled by the custom logic, return `null` to fall back to default logic.
12
+ typedef ToastPositionMapping = Widget ? Function (Widget child, ToastGravity ? gravity);
13
+
6
14
/// Toast Length
7
15
/// Only for Android Platform
8
16
enum Toast {
@@ -233,6 +241,7 @@ class FToast {
233
241
Duration fadeDuration = const Duration (milliseconds: 350 ),
234
242
bool ignorePointer = false ,
235
243
bool isDismissable = false ,
244
+ ToastPositionMapping ? customPositionMapping,
236
245
}) {
237
246
if (context == null )
238
247
throw ("Error: Context is null, Please call init(context) before showing toast." );
@@ -258,6 +267,15 @@ class FToast {
258
267
OverlayEntry newEntry = OverlayEntry (builder: (context) {
259
268
if (positionedToastBuilder != null )
260
269
return positionedToastBuilder (context, newChild);
270
+ // Use custom mapping logic to fall back to the default mapping if it is not defined or returns null
271
+ if (customPositionMapping != null ) {
272
+ Widget ? customPosition = customPositionMapping (newChild, gravity);
273
+ if (customPosition != null ) {
274
+ return customPosition;
275
+ }
276
+ }
277
+
278
+ // Use the default mapping logic
261
279
return _getPostionWidgetBasedOnGravity (newChild, gravity);
262
280
});
263
281
_overlayQueue.add (_ToastEntry (
0 commit comments