Skip to content

Commit

Permalink
[Presentation] The toast are now created by using the build context
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Apr 4, 2021
1 parent edb547c commit 17fa960
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 55 deletions.
Expand Up @@ -12,6 +12,7 @@ class ReorderSessionsDialog extends StatelessWidget {
Widget build(BuildContext context) {
final theme = Theme.of(context);
final s = S.of(context);
final fToast = ToastUtils.of(context);
return AlertDialog(
title: Text(s.priority),
content: Column(
Expand All @@ -34,7 +35,7 @@ class ReorderSessionsDialog extends StatelessWidget {
return ListTile(
key: Key('$index'),
title: Text('#$position - ${session.name}', overflow: TextOverflow.ellipsis),
onTap: () => ToastUtils.showInfoToast(s.holdToReorder),
onTap: () => ToastUtils.showInfoToast(fToast, s.holdToReorder),
);
},
onReorder: (oldIndex, newIndex) => _onReorder(oldIndex, newIndex, context),
Expand Down
Expand Up @@ -12,6 +12,7 @@ class ReorderItemsDialog extends StatelessWidget {
Widget build(BuildContext context) {
final theme = Theme.of(context);
final s = S.of(context);
final fToast = ToastUtils.of(context);
return AlertDialog(
title: Text(s.priority),
content: Column(
Expand All @@ -34,7 +35,7 @@ class ReorderItemsDialog extends StatelessWidget {
return ListTile(
key: Key('$index'),
title: Text('#$position - ${item.name}', overflow: TextOverflow.ellipsis),
onTap: () => ToastUtils.showInfoToast(s.holdToReorder),
onTap: () => ToastUtils.showInfoToast(fToast, s.holdToReorder),
);
},
onReorder: (oldIndex, newIndex) => _onReorder(oldIndex, newIndex, context),
Expand Down
13 changes: 3 additions & 10 deletions lib/presentation/characters/widgets/character_card.dart
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:genshindb/application/bloc.dart';
import 'package:genshindb/domain/enums/enums.dart';
import 'package:genshindb/domain/models/models.dart';
Expand All @@ -12,6 +11,7 @@ import 'package:genshindb/presentation/shared/extensions/element_type_extensions
import 'package:genshindb/presentation/shared/extensions/i18n_extensions.dart';
import 'package:genshindb/presentation/shared/rarity.dart';
import 'package:genshindb/presentation/shared/styles.dart';
import 'package:genshindb/presentation/shared/utils/toast_utils.dart';
import 'package:transparent_image/transparent_image.dart';

import 'character_card_ascension_materials_bottom.dart';
Expand Down Expand Up @@ -133,16 +133,9 @@ class CharacterCard extends StatelessWidget {

Future<void> _gotoCharacterPage(BuildContext context) async {
if (isComingSoon && !isInSelectionMode) {
final theme = Theme.of(context);
final s = S.of(context);
Fluttertoast.showToast(
msg: s.comingSoon,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
backgroundColor: theme.accentColor,
textColor: Colors.white,
fontSize: 16.0,
);
final fToast = ToastUtils.of(context);
ToastUtils.showWarningToast(fToast, s.comingSoon);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/presentation/game_codes/game_codes_page.dart
Expand Up @@ -90,6 +90,7 @@ class GameCodesPage extends StatelessWidget {
}

TableRow _buildRow(S s, BuildContext context, GameCodeModel model) {
final fToast = ToastUtils.of(context);
final rewards = model.rewards.map((m) => WrappedAscensionMaterial(image: m.fullImagePath, quantity: m.quantity, size: 20)).toList();
return TableRow(
children: [
Expand Down Expand Up @@ -122,7 +123,7 @@ class GameCodesPage extends StatelessWidget {
splashRadius: 20,
icon: const Icon(Icons.copy),
onPressed: () => Clipboard.setData(ClipboardData(text: model.code)).then(
(value) => ToastUtils.showInfoToast(s.codeXWasCopied(model.code)),
(value) => ToastUtils.showInfoToast(fToast, s.codeXWasCopied(model.code)),
),
)
],
Expand Down
3 changes: 2 additions & 1 deletion lib/presentation/monsters/widgets/monster_card.dart
Expand Up @@ -37,9 +37,10 @@ class MonsterCard extends StatelessWidget {
Widget build(BuildContext context) {
final theme = Theme.of(context);
final s = S.of(context);
final fToast = ToastUtils.of(context);
return InkWell(
borderRadius: Styles.mainCardBorderRadius,
onTap: () => ToastUtils.showWarningToast(s.comingSoon),
onTap: () => ToastUtils.showWarningToast(fToast, s.comingSoon),
child: Card(
clipBehavior: Clip.hardEdge,
shape: Styles.mainCardShape,
Expand Down
Expand Up @@ -75,7 +75,8 @@ class LanguageSettingsCard extends StatelessWidget {

void _languageChanged(AppLanguageType newValue, BuildContext context) {
final s = S.of(context);
ToastUtils.showInfoToast(s.restartMayBeNeeded);
final fToast = ToastUtils.of(context);
ToastUtils.showInfoToast(fToast, s.restartMayBeNeeded);
context.read<SettingsBloc>().add(SettingsEvent.languageChanged(newValue: newValue));
}
}
3 changes: 2 additions & 1 deletion lib/presentation/shared/circle_monster.dart
Expand Up @@ -17,10 +17,11 @@ class CircleMonster extends StatelessWidget {
@override
Widget build(BuildContext context) {
final s = S.of(context);
final fToast = ToastUtils.of(context);
return CircleItem(
image: image,
radius: radius,
onTap: (_) => ToastUtils.showWarningToast(s.comingSoon),
onTap: (_) => ToastUtils.showWarningToast(fToast, s.comingSoon),
);
}
}
106 changes: 70 additions & 36 deletions lib/presentation/shared/utils/toast_utils.dart
@@ -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),
),
),
],
),
);
}
}
7 changes: 4 additions & 3 deletions lib/presentation/tierlist/tier_list_page.dart
Expand Up @@ -110,19 +110,20 @@ class _TierListPageState extends State<TierListPage> {

Future<void> _takeScreenshot() async {
final s = S.of(context);
final fToast = ToastUtils.of(context);
final bloc = context.read<TierListBloc>();
try {
if (!await Permission.storage.request().isGranted) {
ToastUtils.showInfoToast(s.acceptToSaveImg);
ToastUtils.showInfoToast(fToast, s.acceptToSaveImg);
return;
}

final bytes = await screenshotController.capture(pixelRatio: 1.5);
ImageGallerySaver.saveImage(bytes, quality: 100);
ToastUtils.showSucceedToast(s.imgSavedSuccessfully);
ToastUtils.showSucceedToast(fToast, s.imgSavedSuccessfully);
bloc.add(const TierListEvent.screenshotTaken(succeed: true));
} catch (e, trace) {
ToastUtils.showErrorToast(s.unknownError);
ToastUtils.showErrorToast(fToast, s.unknownError);
bloc.add(TierListEvent.screenshotTaken(succeed: false, ex: e, trace: trace));
}
}
Expand Down

0 comments on commit 17fa960

Please sign in to comment.