Skip to content

Commit 9c5595c

Browse files
authored
Support custom ToastGravity to Position Mapping (#533)
* ✨ add custom position mapping for toasts * 🐛 feat(example): add custom position mapping for toast messages
1 parent 41bcae7 commit 9c5595c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

example/lib/toast_context.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ class _ToastContextState extends State<ToastContext> {
3636
);
3737
}
3838

39+
_showCustomPositionMappingToast() {
40+
final customPositionMapping = (child, gravity) {
41+
switch (gravity) {
42+
case ToastGravity.TOP:
43+
return Positioned(top: 150.0, left: 24.0, right: 24.0, child: child);
44+
case ToastGravity.BOTTOM:
45+
return Positioned(
46+
bottom: 200, left: 24.0, right: 24.0, child: child);
47+
default:
48+
return null;
49+
}
50+
};
51+
fToast.showToast(
52+
child: Text("This is the custom ToastGravity.BOTTOM"),
53+
gravity: ToastGravity.BOTTOM,
54+
toastDuration: Duration(seconds: 2),
55+
customPositionMapping: customPositionMapping,
56+
);
57+
58+
fToast.showToast(
59+
child: Text("This is the default ToastGravity.BOTTOM"),
60+
gravity: ToastGravity.BOTTOM,
61+
toastDuration: Duration(seconds: 2),
62+
);
63+
}
64+
3965
_showBuilderToast() {
4066
fToast.showToast(
4167
child: toast,
@@ -149,6 +175,9 @@ class _ToastContextState extends State<ToastContext> {
149175
_showToast();
150176
},
151177
),
178+
SizedBox(
179+
height: 24.0,
180+
),
152181
ElevatedButton(
153182
child: Text("Show Custom Toast via PositionedToastBuilder"),
154183
onPressed: () {
@@ -158,6 +187,15 @@ class _ToastContextState extends State<ToastContext> {
158187
SizedBox(
159188
height: 24.0,
160189
),
190+
ElevatedButton(
191+
child: Text("Show Custom Toast via CustomPositionMapping"),
192+
onPressed: () {
193+
_showCustomPositionMappingToast();
194+
},
195+
),
196+
SizedBox(
197+
height: 24.0,
198+
),
161199
ElevatedButton(
162200
child: Text("Custom Toast With Close Button"),
163201
onPressed: () {

lib/fluttertoast.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter/services.dart';
55

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+
614
/// Toast Length
715
/// Only for Android Platform
816
enum Toast {
@@ -233,6 +241,7 @@ class FToast {
233241
Duration fadeDuration = const Duration(milliseconds: 350),
234242
bool ignorePointer = false,
235243
bool isDismissable = false,
244+
ToastPositionMapping? customPositionMapping,
236245
}) {
237246
if (context == null)
238247
throw ("Error: Context is null, Please call init(context) before showing toast.");
@@ -258,6 +267,15 @@ class FToast {
258267
OverlayEntry newEntry = OverlayEntry(builder: (context) {
259268
if (positionedToastBuilder != null)
260269
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
261279
return _getPostionWidgetBasedOnGravity(newChild, gravity);
262280
});
263281
_overlayQueue.add(_ToastEntry(

0 commit comments

Comments
 (0)