Convert most bloc usage to listeners and builders

This commit is contained in:
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(
onTap: () => widget.onSuggestionChosen?.call(kanji),
child: Container(
height: fontSize + 2 * suggestionCirclePadding,
width: fontSize + 2 * suggestionCirclePadding,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: BlocProvider.of<ThemeBloc>(context)
.state
.theme
.menuGreyLight
.background,
),
child: Center(
child: Text(
kanji,
style: const TextStyle(fontSize: fontSize),
),
),
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.menuGreyLight;
return Container(
height: fontSize + 2 * suggestionCirclePadding,
width: fontSize + 2 * suggestionCirclePadding,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors.background,
),
child: Center(
child: Text(
kanji,
style: TextStyle(
fontSize: fontSize,
color: colors.foreground,
),
),
),
);
},
),
);

View File

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

View File

@ -11,32 +11,32 @@ class KanjiBox extends StatelessWidget {
}) : super(key: key);
@override
Widget build(BuildContext context) {
final ColorSet menuColors =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight;
return IntrinsicHeight(
child: AspectRatio(
aspectRatio: 1,
child: Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: Center(
child: FittedBox(
child: Text(
kanji,
style: TextStyle(
color: menuColors.foreground,
fontSize: 25,
Widget build(BuildContext context) => IntrinsicHeight(
child: AspectRatio(
aspectRatio: 1,
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.menuGreyLight;
return Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: colors.background,
borderRadius: BorderRadius.circular(10.0),
),
),
),
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);
@override
Widget build(BuildContext context) {
final theme = BlocProvider.of<ThemeBloc>(context).state.theme;
final menuColors = theme.menuGreyNormal;
final kanaColors =
kanaType == _KanaType.kunyomi ? theme.kunyomiColor : theme.onyomiColor;
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final theme = state.theme;
final menuColors = theme.menuGreyNormal;
final kanaColors = kanaType == _KanaType.kunyomi
? theme.kunyomiColor
: theme.onyomiColor;
return Container(
margin: const EdgeInsets.symmetric(
vertical: 5.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: IntrinsicHeight(
child: Row(
children: [
_Kana(colors: kanaColors, example: yomiExample),
_ExampleText(colors: menuColors, example: yomiExample)
],
),
),
);
}
return Container(
margin: const EdgeInsets.symmetric(
vertical: 5.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
color: menuColors.background,
borderRadius: BorderRadius.circular(10.0),
),
child: IntrinsicHeight(
child: Row(
children: [
_Kana(colors: kanaColors, example: yomiExample),
_ExampleText(colors: menuColors, example: yomiExample)
],
),
),
);
},
);
}
class _Kana extends StatelessWidget {

View File

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

View File

@ -11,24 +11,25 @@ class Header extends StatelessWidget {
}) : super(key: key);
@override
Widget build(BuildContext context) {
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
Widget build(BuildContext context) => AspectRatio(
aspectRatio: 1,
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.kanjiResultColor;
return AspectRatio(
aspectRatio: 1,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: colors.background,
return Container(
decoration: BoxDecoration(
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);
@override
Widget build(BuildContext context) {
final colors =
BlocProvider.of<ThemeBloc>(context).state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors.background,
),
child: Text(
jlptLevel ?? ifNullChar,
style: TextStyle(
color: colors.foreground,
fontSize: 20.0,
),
),
);
}
Widget build(BuildContext context) => BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
final colors = state.theme.kanjiResultColor;
return Container(
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors.background,
),
child: Text(
jlptLevel ?? ifNullChar,
style: TextStyle(
color: colors.foreground,
fontSize: 20.0,
),
),
);
},
);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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