2022-07-05 13:11:43 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
class Toast extends StatefulWidget {
|
|
|
|
final String message;
|
|
|
|
final Duration duration;
|
|
|
|
final void Function() onComplete;
|
|
|
|
final Color? backgroundColor;
|
|
|
|
final TextStyle? textStyle;
|
|
|
|
const Toast(
|
|
|
|
this.message,
|
|
|
|
this.duration, {
|
|
|
|
required this.onComplete,
|
|
|
|
this.backgroundColor,
|
|
|
|
this.textStyle,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _ToastState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ToastState extends State<Toast> with SingleTickerProviderStateMixin {
|
|
|
|
late AnimationController _animator;
|
|
|
|
late Tween<double> _tween;
|
|
|
|
late Animation<double> _opacity;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
_animator = AnimationController(
|
|
|
|
duration: const Duration(milliseconds: 100), vsync: this);
|
|
|
|
_tween = Tween(begin: 0, end: 1);
|
|
|
|
_opacity = _tween.animate(_animator)
|
|
|
|
..addListener(() {
|
|
|
|
setState(() {});
|
|
|
|
});
|
|
|
|
|
|
|
|
_animate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _animate() async {
|
|
|
|
await _animator.forward();
|
|
|
|
if (mounted) {
|
|
|
|
await Future.delayed(widget.duration);
|
|
|
|
}
|
|
|
|
if (mounted) {
|
|
|
|
await _animator.reverse();
|
|
|
|
}
|
|
|
|
widget.onComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_animator.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return FadeTransition(
|
|
|
|
opacity: _opacity,
|
|
|
|
child: Material(
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(4.0)),
|
|
|
|
),
|
|
|
|
color: widget.backgroundColor,
|
|
|
|
child: Center(
|
2022-09-02 12:18:18 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
2022-07-05 13:11:43 +03:00
|
|
|
child: Text(
|
2022-09-02 12:18:18 +03:00
|
|
|
widget.message,
|
|
|
|
style: widget.textStyle,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-07-05 13:11:43 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Function() showToast(
|
|
|
|
BuildContext context,
|
|
|
|
String message, {
|
|
|
|
Duration duration = const Duration(seconds: 2),
|
|
|
|
}) {
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
final colorScheme = theme.colorScheme;
|
|
|
|
final bool isThemeDark = theme.brightness == Brightness.dark;
|
|
|
|
final Color backgroundColor = isThemeDark
|
|
|
|
? colorScheme.onSurface
|
|
|
|
: Color.alphaBlend(
|
|
|
|
colorScheme.onSurface.withOpacity(0.80), colorScheme.surface);
|
|
|
|
final textStyle =
|
|
|
|
ThemeData(brightness: isThemeDark ? Brightness.light : Brightness.dark)
|
|
|
|
.textTheme
|
|
|
|
.subtitle1;
|
|
|
|
|
|
|
|
OverlayEntry? entry;
|
2022-07-05 14:26:12 +03:00
|
|
|
void close() {
|
|
|
|
if (entry != null && entry.mounted) {
|
|
|
|
entry.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 13:11:43 +03:00
|
|
|
entry = OverlayEntry(builder: (context) {
|
|
|
|
return Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: Container(
|
|
|
|
height: 50,
|
|
|
|
width: 400,
|
|
|
|
margin: const EdgeInsets.all(8),
|
|
|
|
child: Toast(
|
|
|
|
message,
|
|
|
|
duration,
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
textStyle: textStyle,
|
2022-07-05 14:26:12 +03:00
|
|
|
onComplete: close,
|
2022-07-05 13:11:43 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Timer.run(() {
|
|
|
|
Overlay.of(context)!.insert(entry!);
|
|
|
|
});
|
2022-07-05 14:26:12 +03:00
|
|
|
|
|
|
|
return close;
|
2022-07-05 13:11:43 +03:00
|
|
|
}
|