2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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) {
|
2023-01-10 12:17:31 +03:00
|
|
|
return Positioned(
|
|
|
|
bottom: MediaQuery.of(context).viewInsets.bottom,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
child: SafeArea(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
child: Container(
|
|
|
|
height: 50,
|
|
|
|
width: 400,
|
|
|
|
margin: const EdgeInsets.all(8),
|
|
|
|
child: Toast(
|
|
|
|
message,
|
|
|
|
duration,
|
|
|
|
backgroundColor: backgroundColor,
|
|
|
|
textStyle: textStyle,
|
|
|
|
onComplete: close,
|
|
|
|
),
|
2022-09-14 10:36:22 +03:00
|
|
|
),
|
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
|
|
|
}
|