diff --git a/lib/presentation/screens/bloon/boss_bloon.dart b/lib/presentation/screens/bloon/boss_bloon.dart index 1fde31b..56d607f 100644 --- a/lib/presentation/screens/bloon/boss_bloon.dart +++ b/lib/presentation/screens/bloon/boss_bloon.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'package:btd6wiki/utilities/favorite_state.dart'; +import 'package:btd6wiki/utilities/utils.dart'; import 'package:provider/provider.dart'; import 'package:smooth_page_indicator/smooth_page_indicator.dart'; import 'package:carousel_slider/carousel_slider.dart'; @@ -67,16 +68,8 @@ class _BossBloonState extends State { Consumer( builder: (context, favoriteState, child) { return IconButton( - onPressed: () { - String msg = favoriteState.toggleFavorite(boss); - ScaffoldMessenger.of(context).removeCurrentSnackBar(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Center(child: Text(msg)), - duration: const Duration(milliseconds: 400), - ), - ); - }, + onPressed: () => + toggleFavoriteFunc(context, favoriteState, boss), icon: favoriteState.isFavorite(boss.type, boss.id) ? const Icon(Icons.star) : const Icon(Icons.star_border_outlined), diff --git a/lib/presentation/screens/bloon/single_bloon.dart b/lib/presentation/screens/bloon/single_bloon.dart index 75d4376..1df1601 100644 --- a/lib/presentation/screens/bloon/single_bloon.dart +++ b/lib/presentation/screens/bloon/single_bloon.dart @@ -58,16 +58,8 @@ class _SingleBloonState extends State { Consumer( builder: (context, favoriteState, child) { return IconButton( - onPressed: () { - String msg = favoriteState.toggleFavorite(bloon); - ScaffoldMessenger.of(context).removeCurrentSnackBar(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Center(child: Text(msg)), - duration: const Duration(milliseconds: 400), - ), - ); - }, + onPressed: () => + toggleFavoriteFunc(context, favoriteState, bloon), icon: favoriteState.isFavorite(bloon.type, bloon.id) ? const Icon(Icons.star) : const Icon(Icons.star_border_outlined), diff --git a/lib/presentation/screens/hero/heroes.dart b/lib/presentation/screens/hero/heroes.dart index 6a2f732..4ecd00e 100644 --- a/lib/presentation/screens/hero/heroes.dart +++ b/lib/presentation/screens/hero/heroes.dart @@ -68,9 +68,8 @@ class _HeroesState extends State { return InkWell( borderRadius: BorderRadius.circular(20), - onLongPress: () { - favoriteState.toggleFavorite(hero); - }, + onLongPress: () => + toggleFavoriteFunc(context, favoriteState, hero), onTap: () { widget.analyticsHelper.logEvent( name: widgetEngagement, diff --git a/lib/presentation/screens/hero/single_hero.dart b/lib/presentation/screens/hero/single_hero.dart index 2e57269..61239b2 100644 --- a/lib/presentation/screens/hero/single_hero.dart +++ b/lib/presentation/screens/hero/single_hero.dart @@ -107,16 +107,8 @@ class _SingleHeroState extends State { Consumer( builder: (context, favoriteState, child) { return IconButton( - onPressed: () { - String msg = favoriteState.toggleFavorite(singleHero); - ScaffoldMessenger.of(context).removeCurrentSnackBar(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Center(child: Text(msg)), - duration: const Duration(milliseconds: 400), - ), - ); - }, + onPressed: () => toggleFavoriteFunc( + context, favoriteState, singleHero), icon: favoriteState.isFavorite( singleHero.type, singleHero.id) ? const Icon(Icons.star) diff --git a/lib/presentation/screens/maps/maps.dart b/lib/presentation/screens/maps/maps.dart index af85762..52f2c74 100644 --- a/lib/presentation/screens/maps/maps.dart +++ b/lib/presentation/screens/maps/maps.dart @@ -76,9 +76,8 @@ class _MapsState extends State { return Padding( padding: const EdgeInsets.all(5.0), child: GestureDetector( - onLongPress: () { - favoriteState.toggleFavorite(map); - }, + onLongPress: () => + toggleFavoriteFunc(context, favoriteState, map), onTap: () { widget.analyticsHelper.logEvent( name: widgetEngagement, diff --git a/lib/presentation/screens/maps/single_map.dart b/lib/presentation/screens/maps/single_map.dart index 11916b6..b051b8b 100644 --- a/lib/presentation/screens/maps/single_map.dart +++ b/lib/presentation/screens/maps/single_map.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'package:btd6wiki/utilities/favorite_state.dart'; +import 'package:btd6wiki/utilities/utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; @@ -50,23 +51,15 @@ class _SingleMapState extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: !loading + appBar: !loading ? AppBar( title: Text(map.name), actions: [ Consumer( builder: (context, favoriteState, child) { return IconButton( - onPressed: () { - String msg = favoriteState.toggleFavorite(map); - ScaffoldMessenger.of(context).removeCurrentSnackBar(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Center(child: Text(msg)), - duration: const Duration(milliseconds: 400), - ), - ); - }, + onPressed: () => + toggleFavoriteFunc(context, favoriteState, map), icon: favoriteState.isFavorite(map.type, map.id) ? const Icon(Icons.star) : const Icon(Icons.star_border_outlined), diff --git a/lib/presentation/screens/tower/single_tower.dart b/lib/presentation/screens/tower/single_tower.dart index d634766..fd44cd1 100644 --- a/lib/presentation/screens/tower/single_tower.dart +++ b/lib/presentation/screens/tower/single_tower.dart @@ -76,16 +76,8 @@ class _SingleTowerState extends State { Consumer( builder: (context, favoriteState, child) { return IconButton( - onPressed: () { - String msg = favoriteState.toggleFavorite(tower); - ScaffoldMessenger.of(context).removeCurrentSnackBar(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Center(child: Text(msg)), - duration: const Duration(milliseconds: 400), - ), - ); - }, + onPressed: () => + toggleFavoriteFunc(context, favoriteState, tower), icon: favoriteState.isFavorite(tower.type, tower.id) ? const Icon(Icons.star) : const Icon(Icons.star_border_outlined), diff --git a/lib/presentation/screens/tower/towers.dart b/lib/presentation/screens/tower/towers.dart index 23e9a7a..68aeb60 100644 --- a/lib/presentation/screens/tower/towers.dart +++ b/lib/presentation/screens/tower/towers.dart @@ -69,9 +69,8 @@ class _TowersState extends State { return InkWell( borderRadius: BorderRadius.circular(20), - onLongPress: () { - favoriteState.toggleFavorite(tower); - }, + onLongPress: () => + toggleFavoriteFunc(context, favoriteState, tower), onTap: () { widget.analyticsHelper.logEvent( name: widgetEngagement, @@ -88,7 +87,6 @@ class _TowersState extends State { towerId: tower.id, analyticsHelper: widget.analyticsHelper, ), - ), ); }, diff --git a/lib/presentation/widgets/bloons/bloons_grid.dart b/lib/presentation/widgets/bloons/bloons_grid.dart index e3ce793..ce652cb 100644 --- a/lib/presentation/widgets/bloons/bloons_grid.dart +++ b/lib/presentation/widgets/bloons/bloons_grid.dart @@ -1,3 +1,4 @@ +import 'package:btd6wiki/utilities/utils.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '/models/base_model.dart'; @@ -37,9 +38,8 @@ class BloonsGrid extends StatelessWidget { return Consumer( builder: (context, favoriteState, child) { return InkWell( - onLongPress: () { - favoriteState.toggleFavorite(bloon); - }, + onLongPress: () => + toggleFavoriteFunc(context, favoriteState, bloon), onTap: () { analyticsHelper.logEvent( name: widgetEngagement, diff --git a/lib/presentation/widgets/bloons/bosses_grid.dart b/lib/presentation/widgets/bloons/bosses_grid.dart index 9c6fa21..f97d7d2 100644 --- a/lib/presentation/widgets/bloons/bosses_grid.dart +++ b/lib/presentation/widgets/bloons/bosses_grid.dart @@ -1,3 +1,4 @@ +import 'package:btd6wiki/utilities/utils.dart'; import 'package:provider/provider.dart'; import 'package:flutter/material.dart'; import '/analytics/analytics_constants.dart'; @@ -36,9 +37,8 @@ class BossesGrid extends StatelessWidget { return Consumer( builder: (context, favoriteState, child) { return InkWell( - onLongPress: () { - favoriteState.toggleFavorite(boss); - }, + onLongPress: () => + toggleFavoriteFunc(context, favoriteState, boss), onTap: () { analyticsHelper.logEvent( name: widgetEngagement, diff --git a/lib/utilities/constants.dart b/lib/utilities/constants.dart index a59576e..6e7a76c 100644 --- a/lib/utilities/constants.dart +++ b/lib/utilities/constants.dart @@ -15,6 +15,8 @@ const int kMapsIndex = 3; const String kFavorite = 'favorite'; +const Duration snackBarDuration = Duration(milliseconds: 850); + const List capTitles = [ 'Towers', 'Heroes', diff --git a/lib/utilities/utils.dart b/lib/utilities/utils.dart index 9ccd435..6415e60 100644 --- a/lib/utilities/utils.dart +++ b/lib/utilities/utils.dart @@ -1,3 +1,4 @@ +import 'package:btd6wiki/utilities/favorite_state.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:flutter/material.dart'; import '/analytics/analytics_constants.dart'; @@ -95,6 +96,18 @@ void navigateToPage(BuildContext context, var item, ); } +void toggleFavoriteFunc( + BuildContext context, FavoriteState favoriteState, var item) { + String msg = favoriteState.toggleFavorite(item); + ScaffoldMessenger.of(context).removeCurrentSnackBar(); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Center(child: Text(msg)), + duration: snackBarDuration, + ), + ); +} + String formatBigNumber(int number) { if (number < 1000) { return number.toString();