Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Presentation] The toast are now created by using the build context
- Loading branch information
Showing
9 changed files
with
89 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,86 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:fluttertoast/fluttertoast.dart'; | ||
|
||
enum ToastType { | ||
info, | ||
succeed, | ||
warning, | ||
error, | ||
} | ||
|
||
class ToastUtils { | ||
static void showSucceedToast( | ||
String msg, { | ||
Color textColor = Colors.white, | ||
Color bgColor = Colors.green, | ||
}) { | ||
_showToast(msg, textColor, bgColor); | ||
static FToast of(BuildContext context) { | ||
final fToast = FToast(); | ||
fToast.init(context); | ||
return fToast; | ||
} | ||
|
||
static void showInfoToast( | ||
String msg, { | ||
Color textColor = Colors.white, | ||
Color bgColor = Colors.blue, | ||
}) { | ||
_showToast(msg, textColor, bgColor); | ||
} | ||
static void showSucceedToast(FToast toast, String msg) => _showToast(toast, msg, Colors.white, ToastType.succeed); | ||
|
||
static void showWarningToast( | ||
String msg, { | ||
Color textColor = Colors.white, | ||
Color bgColor = Colors.orange, | ||
}) { | ||
_showToast(msg, textColor, bgColor); | ||
} | ||
static void showInfoToast(FToast toast, String msg) => _showToast(toast, msg, Colors.white, ToastType.info); | ||
|
||
static void showErrorToast( | ||
String msg, { | ||
Color textColor = Colors.white, | ||
Color bgColor = Colors.red, | ||
}) { | ||
_showToast(msg, textColor, bgColor); | ||
} | ||
static void showWarningToast(FToast toast, String msg) => _showToast(toast, msg, Colors.white, ToastType.warning); | ||
|
||
static void showErrorToast(FToast toast, String msg) => _showToast(toast, msg, Colors.white, ToastType.error); | ||
|
||
static void _showToast( | ||
FToast toast, | ||
String msg, | ||
Color textColor, | ||
Color bgColor, { | ||
Toast length = Toast.LENGTH_SHORT, | ||
}) { | ||
Fluttertoast.showToast( | ||
msg: msg, | ||
toastLength: length, | ||
ToastType type, | ||
) { | ||
Color bgColor; | ||
Icon icon; | ||
switch (type) { | ||
case ToastType.info: | ||
bgColor = Colors.blue; | ||
icon = const Icon(Icons.info); | ||
break; | ||
case ToastType.succeed: | ||
bgColor = Colors.green; | ||
icon = const Icon(Icons.check); | ||
break; | ||
case ToastType.warning: | ||
bgColor = Colors.orange; | ||
icon = const Icon(Icons.warning); | ||
break; | ||
case ToastType.error: | ||
bgColor = Colors.red; | ||
icon = const Icon(Icons.dangerous); | ||
break; | ||
default: | ||
throw Exception('Invalid toast type = $type'); | ||
} | ||
|
||
final widget = _buildToast(msg, textColor, bgColor, icon, toast.context); | ||
toast.showToast( | ||
child: widget, | ||
gravity: ToastGravity.BOTTOM, | ||
backgroundColor: bgColor, | ||
textColor: textColor, | ||
fontSize: 16.0, | ||
toastDuration: const Duration(seconds: 2), | ||
); | ||
} | ||
|
||
static Widget _buildToast(String msg, Color textColor, Color bgColor, Icon icon, BuildContext context) { | ||
return Container( | ||
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0), | ||
decoration: BoxDecoration( | ||
borderRadius: BorderRadius.circular(25.0), | ||
color: bgColor, | ||
), | ||
child: Row( | ||
mainAxisSize: MainAxisSize.min, | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
icon, | ||
const SizedBox(width: 10.0), | ||
Flexible( | ||
child: Text( | ||
msg, | ||
style: TextStyle(color: textColor), | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |
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