Convert most bloc usage to listeners and builders

add-more-content-to-search-results^2
Oystein Kristoffer Tveit 2022-01-23 04:52:28 +01:00
parent 7e8442881a
commit 25a58d6f59
14 changed files with 341 additions and 321 deletions

View File

@ -95,23 +95,28 @@ class _DrawingBoardState extends State<DrawingBoard> {
Widget kanjiChip(String kanji) => InkWell( Widget kanjiChip(String kanji) => InkWell(
onTap: () => widget.onSuggestionChosen?.call(kanji), onTap: () => widget.onSuggestionChosen?.call(kanji),
child: Container( child: BlocBuilder<ThemeBloc, ThemeState>(
height: fontSize + 2 * suggestionCirclePadding, builder: (context, state) {
width: fontSize + 2 * suggestionCirclePadding, final colors = state.theme.menuGreyLight;
decoration: BoxDecoration(
shape: BoxShape.circle, return Container(
color: BlocProvider.of<ThemeBloc>(context) height: fontSize + 2 * suggestionCirclePadding,
.state width: fontSize + 2 * suggestionCirclePadding,
.theme decoration: BoxDecoration(
.menuGreyLight shape: BoxShape.circle,
.background, color: colors.background,
), ),
child: Center( child: Center(
child: Text( child: Text(
kanji, kanji,
style: const TextStyle(fontSize: fontSize), style: TextStyle(
), fontSize: fontSize,
), color: colors.foreground,
),
),
),
);
},
), ),
); );

View File

@ -36,23 +36,23 @@ class DateDivider extends StatelessWidget {
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final Widget header = builder: (context, state) {
(text != null) ? Text(text!) : Text(getHumanReadableDate(date!)); final colors = state.theme.menuGreyNormal;
final ColorSet _menuColors = return Container(
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyNormal; decoration: BoxDecoration(color: colors.background),
padding: const EdgeInsets.symmetric(
return Container( vertical: 5,
decoration: BoxDecoration(color: _menuColors.background), horizontal: 10,
padding: const EdgeInsets.symmetric( ),
vertical: 5, child: DefaultTextStyle.merge(
horizontal: 10, child: (text != null)
), ? Text(text!)
child: DefaultTextStyle.merge( : Text(getHumanReadableDate(date!)),
child: header, style: TextStyle(color: colors.foreground),
style: TextStyle(color: _menuColors.foreground), ),
), );
); },
} );
} }

View File

@ -11,32 +11,32 @@ class KanjiBox extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => IntrinsicHeight(
final ColorSet menuColors = child: AspectRatio(
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight; aspectRatio: 1,
child: BlocBuilder<ThemeBloc, ThemeState>(
return IntrinsicHeight( builder: (context, state) {
child: AspectRatio( final colors = state.theme.menuGreyLight;
aspectRatio: 1, return Container(
child: Container( padding: const EdgeInsets.all(5),
padding: const EdgeInsets.all(5), decoration: BoxDecoration(
decoration: BoxDecoration( color: colors.background,
color: menuColors.background, borderRadius: BorderRadius.circular(10.0),
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: FittedBox(
child: Text(
kanji,
style: TextStyle(
color: menuColors.foreground,
fontSize: 25,
), ),
), child: Center(
), child: FittedBox(
child: Text(
kanji,
style: TextStyle(
color: colors.foreground,
fontSize: 25,
),
),
),
),
);
},
), ),
), ),
), );
);
}
} }

View File

@ -53,31 +53,34 @@ class _Example extends StatelessWidget {
const _Example(this.yomiExample, this.kanaType); const _Example(this.yomiExample, this.kanaType);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final theme = BlocProvider.of<ThemeBloc>(context).state.theme; builder: (context, state) {
final menuColors = theme.menuGreyNormal; final theme = state.theme;
final kanaColors = final menuColors = theme.menuGreyNormal;
kanaType == _KanaType.kunyomi ? theme.kunyomiColor : theme.onyomiColor; final kanaColors = kanaType == _KanaType.kunyomi
? theme.kunyomiColor
: theme.onyomiColor;
return Container( return Container(
margin: const EdgeInsets.symmetric( margin: const EdgeInsets.symmetric(
vertical: 5.0, vertical: 5.0,
horizontal: 10.0, horizontal: 10.0,
), ),
decoration: BoxDecoration( decoration: BoxDecoration(
color: menuColors.background, color: menuColors.background,
borderRadius: BorderRadius.circular(10.0), borderRadius: BorderRadius.circular(10.0),
), ),
child: IntrinsicHeight( child: IntrinsicHeight(
child: Row( child: Row(
children: [ children: [
_Kana(colors: kanaColors, example: yomiExample), _Kana(colors: kanaColors, example: yomiExample),
_ExampleText(colors: menuColors, example: yomiExample) _ExampleText(colors: menuColors, example: yomiExample)
], ],
), ),
), ),
); );
} },
);
} }
class _Kana extends StatelessWidget { class _Kana extends StatelessWidget {

View File

@ -13,23 +13,24 @@ class Grade extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final colors = builder: (context, state) {
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; final colors = state.theme.kanjiResultColor;
return Container( return Container(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration( decoration: BoxDecoration(
color: colors.background, color: colors.background,
shape: BoxShape.circle, shape: BoxShape.circle,
), ),
child: Text( child: Text(
grade ?? ifNullChar, grade ?? ifNullChar,
style: TextStyle( style: TextStyle(
color: colors.foreground, color: colors.foreground,
fontSize: 20.0, fontSize: 20.0,
), ),
), ),
); );
} },
);
} }

View File

@ -11,24 +11,25 @@ class Header extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => AspectRatio(
final colors = aspectRatio: 1,
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.kanjiResultColor;
return AspectRatio( return Container(
aspectRatio: 1, decoration: BoxDecoration(
child: Container( borderRadius: BorderRadius.circular(10.0),
decoration: BoxDecoration( color: colors.background,
borderRadius: BorderRadius.circular(10.0), ),
color: colors.background, child: Center(
child: Text(
kanji,
style: TextStyle(fontSize: 70.0, color: colors.foreground),
),
),
);
},
), ),
child: Center( );
child: Text(
kanji,
style: TextStyle(fontSize: 70.0, color: colors.foreground),
),
),
),
);
}
} }

View File

@ -13,23 +13,23 @@ class JlptLevel extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final colors = builder: (context, state) {
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; final colors = state.theme.kanjiResultColor;
return Container(
return Container( padding: const EdgeInsets.all(10.0),
padding: const EdgeInsets.all(10.0), decoration: BoxDecoration(
decoration: BoxDecoration( shape: BoxShape.circle,
shape: BoxShape.circle, color: colors.background,
color: colors.background, ),
), child: Text(
child: Text( jlptLevel ?? ifNullChar,
jlptLevel ?? ifNullChar, style: TextStyle(
style: TextStyle( color: colors.foreground,
color: colors.foreground, fontSize: 20.0,
fontSize: 20.0, ),
), ),
), );
); },
} );
} }

View File

@ -6,25 +6,30 @@ import '../../../bloc/theme/theme_bloc.dart';
class Radical extends StatelessWidget { class Radical extends StatelessWidget {
final jisho.Radical radical; final jisho.Radical radical;
const Radical({required this.radical, Key? key,}) : super(key: key); const Radical({
required this.radical,
Key? key,
}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final colors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; builder: (context, state) {
final colors = state.theme.kanjiResultColor;
return Container( return Container(
padding: const EdgeInsets.all(15.0), padding: const EdgeInsets.all(15.0),
decoration: BoxDecoration( decoration: BoxDecoration(
shape: BoxShape.circle, shape: BoxShape.circle,
color: colors.background, color: colors.background,
), ),
child: Text( child: Text(
radical.symbol, radical.symbol,
style: TextStyle( style: TextStyle(
color: colors.foreground, color: colors.foreground,
fontSize: 40.0, fontSize: 40.0,
), ),
), ),
); );
} },
);
} }

View File

@ -13,24 +13,25 @@ class Rank extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final colors = builder: (context, state) {
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; final colors = state.theme.kanjiResultColor;
return Container( return Container(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration( decoration: BoxDecoration(
shape: (rank == null) ? BoxShape.circle : BoxShape.rectangle, shape: (rank == null) ? BoxShape.circle : BoxShape.rectangle,
borderRadius: (rank == null) ? null : BorderRadius.circular(10.0), borderRadius: (rank == null) ? null : BorderRadius.circular(10.0),
color: colors.background, color: colors.background,
), ),
child: Text( child: Text(
rank != null ? '${rank.toString()} / 2500' : ifNullChar, rank != null ? '${rank.toString()} / 2500' : ifNullChar,
style: TextStyle( style: TextStyle(
color: colors.foreground, color: colors.foreground,
fontSize: 20.0, fontSize: 20.0,
), ),
), ),
); );
} },
);
} }

View File

@ -5,24 +5,26 @@ import '../../../bloc/theme/theme_bloc.dart';
class StrokeOrderGif extends StatelessWidget { class StrokeOrderGif extends StatelessWidget {
final String uri; final String uri;
const StrokeOrderGif({required this.uri, Key? key,}) : super(key: key); const StrokeOrderGif({
required this.uri,
Key? key,
}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final colors = BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor; builder: (context, state) {
return Container(
return Container( margin: const EdgeInsets.symmetric(vertical: 20.0),
margin: const EdgeInsets.symmetric(vertical: 20.0), padding: const EdgeInsets.all(5.0),
padding: const EdgeInsets.all(5.0), decoration: BoxDecoration(
decoration: BoxDecoration( color: state.theme.kanjiResultColor.background,
color: colors.background, borderRadius: BorderRadius.circular(15.0),
borderRadius: BorderRadius.circular(15.0), ),
), child: ClipRRect(
child: ClipRRect( borderRadius: BorderRadius.circular(10.0),
borderRadius: BorderRadius.circular(10.0), child: Image.network(uri),
child: Image.network(uri), ),
), );
); },
} );
} }

View File

@ -23,6 +23,7 @@ extension on YomiType {
} }
ColorSet getColors(BuildContext context) { ColorSet getColors(BuildContext context) {
// TODO: convert this into a blocbuilder or bloclistener
final theme = BlocProvider.of<ThemeBloc>(context).state.theme; final theme = BlocProvider.of<ThemeBloc>(context).state.theme;
switch (this) { switch (this) {

View File

@ -20,12 +20,14 @@ class KanjiSearchOptionsBar extends StatelessWidget {
const SizedBox(width: 10), const SizedBox(width: 10),
_IconButton( _IconButton(
icon: const Icon(Icons.school), icon: const Icon(Icons.school),
onPressed: () => Navigator.pushNamed(context, Routes.kanjiSearchGrade), onPressed: () =>
Navigator.pushNamed(context, Routes.kanjiSearchGrade),
), ),
const SizedBox(width: 10), const SizedBox(width: 10),
_IconButton( _IconButton(
icon: const Icon(Icons.mode), icon: const Icon(Icons.mode),
onPressed: () => Navigator.pushNamed(context, Routes.kanjiSearchDraw), onPressed: () =>
Navigator.pushNamed(context, Routes.kanjiSearchDraw),
), ),
], ],
), ),
@ -44,12 +46,12 @@ class _IconButton extends StatelessWidget {
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
return IconButton( builder: (context, state) => IconButton(
onPressed: onPressed, onPressed: onPressed,
icon: icon, icon: icon,
iconSize: 30, iconSize: 30,
color: BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyDark.background, color: state.theme.menuGreyDark.background,
); ),
} );
} }

View File

@ -156,15 +156,13 @@ class _KanjiRadicalSearchState extends State<KanjiRadicalSearch> {
Expanded( Expanded(
child: (suggestions.isEmpty) child: (suggestions.isEmpty)
? Center( ? Center(
child: Text( child: BlocBuilder<ThemeBloc, ThemeState>(
'Toggle a radical to start', builder: (context, state) => Text(
style: TextStyle( 'Toggle a radical to start',
fontSize: fontSize * 0.8, style: TextStyle(
color: BlocProvider.of<ThemeBloc>(context) fontSize: fontSize * 0.8,
.state color: state.theme.menuGreyNormal.background,
.theme ),
.menuGreyNormal
.background,
), ),
), ),
) )

View File

@ -37,113 +37,114 @@ class _SettingsViewState extends State<SettingsView> {
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
final TextStyle _titleTextStyle = TextStyle( builder: (context, state) {
color: BlocProvider.of<ThemeBloc>(context).state is DarkThemeState final TextStyle _titleTextStyle = TextStyle(
? AppTheme.jishoGreen.background color:
: null, state is DarkThemeState ? AppTheme.jishoGreen.background : null,
); );
return SettingsList( return SettingsList(
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
contentPadding: const EdgeInsets.symmetric(vertical: 10), contentPadding: const EdgeInsets.symmetric(vertical: 10),
sections: <SettingsSection>[ sections: <SettingsSection>[
SettingsSection( SettingsSection(
title: 'Dictionary', title: 'Dictionary',
titleTextStyle: _titleTextStyle, titleTextStyle: _titleTextStyle,
tiles: <SettingsTile>[ tiles: <SettingsTile>[
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Use romaji', title: 'Use romaji',
onToggle: (b) { onToggle: (b) {
setState(() => romajiEnabled = b); setState(() => romajiEnabled = b);
}, },
switchValue: romajiEnabled, switchValue: romajiEnabled,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
], ],
), ),
SettingsSection( SettingsSection(
title: 'Theme', title: 'Theme',
titleTextStyle: _titleTextStyle, titleTextStyle: _titleTextStyle,
tiles: <SettingsTile>[ tiles: <SettingsTile>[
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Automatically determine theme', title: 'Automatically determine theme',
onToggle: toggleAutoTheme, onToggle: toggleAutoTheme,
switchValue: autoThemeEnabled, switchValue: autoThemeEnabled,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Dark Theme', title: 'Dark Theme',
onToggle: (b) { onToggle: (b) {
BlocProvider.of<ThemeBloc>(context) BlocProvider.of<ThemeBloc>(context)
.add(SetTheme(themeIsDark: b)); .add(SetTheme(themeIsDark: b));
setState(() => darkThemeEnabled = b); setState(() => darkThemeEnabled = b);
}, },
switchValue: darkThemeEnabled, switchValue: darkThemeEnabled,
enabled: !autoThemeEnabled, enabled: !autoThemeEnabled,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
], ],
), ),
SettingsSection( SettingsSection(
title: 'Cache', title: 'Cache',
titleTextStyle: _titleTextStyle, titleTextStyle: _titleTextStyle,
tiles: <SettingsTile>[ tiles: <SettingsTile>[
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Cache grade 1-7 kanji', title: 'Cache grade 1-7 kanji',
switchValue: false, switchValue: false,
onToggle: (v) {}, onToggle: (v) {},
enabled: false, enabled: false,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Cache grade standard kanji', title: 'Cache grade standard kanji',
switchValue: false, switchValue: false,
onToggle: (v) {}, onToggle: (v) {},
enabled: false, enabled: false,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Cache all favourites', title: 'Cache all favourites',
switchValue: false, switchValue: false,
onToggle: (v) {}, onToggle: (v) {},
enabled: false, enabled: false,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
SettingsTile.switchTile( SettingsTile.switchTile(
title: 'Cache all searches', title: 'Cache all searches',
switchValue: false, switchValue: false,
onToggle: (v) {}, onToggle: (v) {},
enabled: false, enabled: false,
switchActiveColor: AppTheme.jishoGreen.background, switchActiveColor: AppTheme.jishoGreen.background,
), ),
], ],
), ),
SettingsSection( SettingsSection(
title: 'Data', title: 'Data',
titleTextStyle: _titleTextStyle, titleTextStyle: _titleTextStyle,
tiles: <SettingsTile>[ tiles: <SettingsTile>[
SettingsTile( SettingsTile(
leading: const Icon(Icons.file_download), leading: const Icon(Icons.file_download),
title: 'Export Data', title: 'Export Data',
enabled: false, enabled: false,
), ),
SettingsTile( SettingsTile(
leading: const Icon(Icons.delete), leading: const Icon(Icons.delete),
title: 'Clear History', title: 'Clear History',
onPressed: clearHistory, onPressed: clearHistory,
titleTextStyle: const TextStyle(color: Colors.red), titleTextStyle: const TextStyle(color: Colors.red),
), ),
SettingsTile( SettingsTile(
leading: const Icon(Icons.delete), leading: const Icon(Icons.delete),
title: 'Clear Favourites', title: 'Clear Favourites',
onPressed: (c) {}, onPressed: (c) {},
titleTextStyle: const TextStyle(color: Colors.red), titleTextStyle: const TextStyle(color: Colors.red),
enabled: false, enabled: false,
) )
], ],
), ),
], ],
); );
} },
);
} }