Skip to content

Commit 5be6d24

Browse files
authored
✨ Allow persist toast (#85)
1 parent fbd0ded commit 5be6d24

File tree

4 files changed

+73
-64
lines changed

4 files changed

+73
-64
lines changed

example/lib/main.dart

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class _MyHomePageState extends State<MyHomePage> {
4848

4949
void _incrementCounter() {
5050
_counter++;
51-
5251
setState(() {});
5352
}
5453

@@ -103,66 +102,72 @@ class _MyHomePageState extends State<MyHomePage> {
103102
});
104103
}
105104

105+
ToastFuture? _persistToast;
106+
107+
void _showPersistToast() {
108+
_persistToast = showToastWidget(
109+
Center(
110+
child: ElevatedButton(
111+
onPressed: () => _persistToast?.dismiss(),
112+
child: const Text('Click this button to dismiss'),
113+
),
114+
),
115+
duration: Duration.zero,
116+
handleTouch: true,
117+
);
118+
}
119+
106120
@override
107121
Widget build(BuildContext context) {
108122
return Scaffold(
109123
appBar: AppBar(title: const Text('Example for OKToast')),
110-
body: Stack(
111-
children: <Widget>[
112-
Center(
113-
child: ListView(
114-
children: <Widget>[
115-
const Text('You have pushed the button this many times:'),
116-
Text(
117-
'$_counter',
118-
style: Theme.of(context).textTheme.headline4,
119-
),
120-
Padding(
121-
padding: const EdgeInsets.all(8.0),
122-
child: Tooltip(
123-
message: 'Toast status when using this to test routing.',
124-
child: ElevatedButton(
125-
child: const Text('New page'),
126-
onPressed: () {
127-
Navigator.push(
128-
context,
129-
MaterialPageRoute<void>(
130-
builder: (_) => const MyHomePage(),
131-
),
132-
);
133-
},
134-
),
135-
),
136-
),
137-
Padding(
138-
padding: const EdgeInsets.all(8.0),
139-
child: Tooltip(
140-
message: 'Add number.',
141-
child: ElevatedButton(
142-
onPressed: _incrementCounter,
143-
child: const Text('Add'),
144-
),
145-
),
146-
),
147-
Padding(
148-
padding: const EdgeInsets.all(8.0),
149-
child: Tooltip(
150-
message: 'Show toast.',
151-
child: ElevatedButton(
152-
onPressed: _showToast,
153-
child: const Text('Toast'),
154-
),
155-
),
156-
),
157-
const TextField(
158-
decoration: InputDecoration(
159-
hintText: 'Use TextField to test the toast of soft keys.',
160-
),
161-
),
162-
],
124+
body: Center(
125+
child: Column(
126+
mainAxisAlignment: MainAxisAlignment.center,
127+
children: <Widget>[
128+
const Center(
129+
child: Text('You have pushed the button this many times:'),
163130
),
164-
),
165-
],
131+
Center(
132+
child: Text(
133+
'$_counter',
134+
style: Theme.of(context).textTheme.headline4,
135+
),
136+
),
137+
Padding(
138+
padding: const EdgeInsets.all(8.0),
139+
child: ElevatedButton(
140+
onPressed: _showToast,
141+
child: const Text('Show toast'),
142+
),
143+
),
144+
Padding(
145+
padding: const EdgeInsets.all(8.0),
146+
child: ElevatedButton(
147+
onPressed: _showPersistToast,
148+
child: const Text('Show a persist toast'),
149+
),
150+
),
151+
Padding(
152+
padding: const EdgeInsets.all(8.0),
153+
child: ElevatedButton(
154+
child: const Text('Toast during pushing to a new page'),
155+
onPressed: () {
156+
_showToast();
157+
Navigator.push(
158+
context,
159+
MaterialPageRoute<void>(builder: (_) => const MyHomePage()),
160+
);
161+
},
162+
),
163+
),
164+
],
165+
),
166+
),
167+
floatingActionButton: FloatingActionButton(
168+
onPressed: _incrementCounter,
169+
child: const Icon(Icons.add),
170+
tooltip: 'Add number',
166171
),
167172
);
168173
}

lib/src/core/toast.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,11 @@ ToastFuture showToastWidget(
144144
animationDuration,
145145
);
146146

147-
future.timer = Timer(duration, () {
148-
future.dismiss();
149-
});
147+
if (duration != Duration.zero) {
148+
future.timer = Timer(duration, () {
149+
future.dismiss();
150+
});
151+
}
150152

151153
Overlay.of(context)?.insert(entry);
152154
ToastManager().addFuture(future);

lib/src/core/toast_future.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ToastFuture {
1414
final GlobalKey<__ToastContainerState> _containerKey;
1515
final Duration animationDuration;
1616

17-
late Timer timer;
17+
Timer? timer;
1818
bool _isShow = true;
1919

2020
void dismiss({bool showAnim = false}) {
@@ -32,6 +32,6 @@ class ToastFuture {
3232
_entry.remove();
3333
}
3434

35-
timer.cancel();
35+
timer?.cancel();
3636
}
3737
}

lib/src/widget/container.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ class __ToastContainerState extends State<_ToastContainer>
4747
Future<void>.delayed(const Duration(milliseconds: 30), () {
4848
_animateTo(1.0);
4949
});
50-
Future<void>.delayed(widget.duration - animationDuration, () {
51-
_animateTo(0.0);
52-
});
50+
if (widget.duration != Duration.zero) {
51+
Future<void>.delayed(widget.duration - animationDuration, () {
52+
_animateTo(0.0);
53+
});
54+
}
5355
}
5456

5557
@override

0 commit comments

Comments
 (0)