Created
April 10, 2020 01:50
-
-
Save 410063005/a465e32c8b73053cdcd6a48f453ad548 to your computer and use it in GitHub Desktop.
A fixed version of FlutterToast. It adds customization options to the original FlutterToast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:toast/toast.dart'; | |
class FixedToastView { | |
static final FixedToastView _singleton = new FixedToastView._internal(); | |
factory FixedToastView() { | |
return _singleton; | |
} | |
FixedToastView._internal(); | |
static OverlayState overlayState; | |
static OverlayEntry _overlayEntry; | |
static bool _isVisible = false; | |
static void show(String msg, BuildContext context, | |
{int duration = 1, | |
int gravity = 0, | |
Color backgroundColor = const Color(0xAA000000), | |
textStyle = const TextStyle(fontSize: 15, color: Colors.white), | |
double backgroundRadius = 4, | |
Border border}) { | |
FixedToastView.dismiss(); | |
FixedToastView.createView(msg + ' ' + msg, context, duration, gravity, | |
backgroundColor, textStyle, backgroundRadius, border); | |
} | |
static void showCustom(Widget content, BuildContext context, | |
{int duration = 1, | |
int gravity = 0, | |
Color backgroundColor = Colors.transparent, | |
double backgroundRadius = 4, | |
Border border}) { | |
FixedToastView.dismiss(); | |
var v = Container( | |
alignment: Alignment.center, | |
width: MediaQuery.of(context).size.width, | |
child: Container( | |
decoration: BoxDecoration( | |
color: backgroundColor, | |
borderRadius: BorderRadius.circular(backgroundRadius), | |
border: border, | |
), | |
//margin: EdgeInsets.symmetric(horizontal: 20), | |
//padding: EdgeInsets.fromLTRB(16, 10, 16, 10), | |
child: content, | |
)); | |
FixedToastView._createView(v, context, duration, gravity, backgroundColor, | |
backgroundRadius, border); | |
} | |
static void createView( | |
String msg, | |
BuildContext context, | |
int duration, | |
int gravity, | |
Color background, | |
TextStyle textStyle, | |
double backgroundRadius, | |
Border border) async { | |
_createView( | |
_createText( | |
msg, context, background, textStyle, backgroundRadius, border), | |
context, | |
duration, | |
gravity, | |
background, | |
backgroundRadius, | |
border); | |
} | |
static void _createView( | |
Widget content, | |
BuildContext context, | |
int duration, | |
int gravity, | |
Color background, | |
double backgroundRadius, | |
Border border) async { | |
overlayState = Overlay.of(context); | |
Paint paint = Paint(); | |
paint.strokeCap = StrokeCap.square; | |
paint.color = background; | |
_overlayEntry = new OverlayEntry( | |
builder: (BuildContext context) => ToastWidget( | |
widget: Container( | |
width: MediaQuery.of(context).size.width, | |
child: content, | |
), | |
gravity: gravity), | |
); | |
_isVisible = true; | |
overlayState.insert(_overlayEntry); | |
await new Future.delayed( | |
Duration(seconds: duration == null ? 1 : duration)); | |
dismiss(); | |
} | |
static Widget _createText(String msg, BuildContext context, Color background, | |
TextStyle textStyle, double backgroundRadius, Border border) { | |
return Container( | |
alignment: Alignment.center, | |
width: MediaQuery.of(context).size.width, | |
child: Container( | |
decoration: BoxDecoration( | |
color: background, | |
borderRadius: BorderRadius.circular(backgroundRadius), | |
border: border, | |
), | |
margin: EdgeInsets.symmetric(horizontal: 20), | |
padding: EdgeInsets.fromLTRB(16, 10, 16, 10), | |
child: Text(msg, softWrap: true, style: textStyle), | |
)); | |
} | |
static dismiss() async { | |
if (!_isVisible) { | |
return; | |
} | |
_isVisible = false; | |
_overlayEntry?.remove(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment