Jisho-Study-Tool/lib/components/kanji/kanji_result_body/yomi_chips.dart

126 lines
2.9 KiB
Dart
Raw Permalink Normal View History

2021-08-08 23:16:54 +02:00
import 'package:flutter/material.dart';
2021-12-01 23:09:53 +01:00
2022-01-19 02:10:05 +01:00
import '../../../bloc/theme/theme_bloc.dart';
import '../../../services/romaji_transliteration.dart';
import '../../../settings.dart';
2021-08-08 23:16:54 +02:00
enum YomiType {
onyomi,
kunyomi,
meaning,
}
extension on YomiType {
String get title {
switch (this) {
case YomiType.onyomi:
return 'Onyomi';
case YomiType.kunyomi:
return 'Kunyomi';
case YomiType.meaning:
return 'Meanings';
}
}
ColorSet getColors(BuildContext context) {
// TODO: convert this into a blocbuilder or bloclistener
2021-08-08 23:16:54 +02:00
final theme = BlocProvider.of<ThemeBloc>(context).state.theme;
switch (this) {
case YomiType.onyomi:
return theme.onyomiColor;
case YomiType.kunyomi:
return theme.kunyomiColor;
case YomiType.meaning:
return theme.menuGreyNormal;
}
}
}
class YomiChips extends StatelessWidget {
final List<String> yomi;
final YomiType type;
2021-12-01 23:09:53 +01:00
const YomiChips({
required this.yomi,
required this.type,
Key? key,
}) : super(key: key);
2021-08-08 23:16:54 +02:00
2021-12-01 23:09:53 +01:00
bool get isExpandable => yomi.length > 6;
2021-08-08 23:16:54 +02:00
2021-12-01 23:09:53 +01:00
Widget yomiCard({
required String yomi,
required ColorSet colors,
}) =>
Container(
2022-01-19 02:10:05 +01:00
margin: const EdgeInsets.symmetric(horizontal: 5),
2021-12-01 23:09:53 +01:00
padding: const EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 10.0,
),
decoration: BoxDecoration(
2022-01-19 02:10:05 +01:00
color: colors.background,
2021-12-01 23:09:53 +01:00
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
yomi,
style: TextStyle(
fontSize: 20.0,
color: colors.foreground,
),
),
);
2022-01-23 03:18:05 +01:00
2021-12-01 23:09:53 +01:00
Widget yomiWrapper(BuildContext context) {
final yomiCards = yomi
.map((y) => romajiEnabled ? transliterateKanaToLatin(y) : y)
2022-01-19 02:10:05 +01:00
.map((y) => yomiCard(yomi: y, colors: type.getColors(context)))
2021-08-08 23:16:54 +02:00
.toList();
2022-01-19 02:10:05 +01:00
final yomiCardsWithTitle = <Widget>[
2022-01-23 03:18:05 +01:00
if (type != YomiType.meaning)
yomiCard(
yomi: type == YomiType.kunyomi ? 'Kun:' : 'On:',
colors: ColorSet(
foreground: type.getColors(context).background,
background: Colors.transparent,
),
),
...yomiCards
];
2022-01-19 02:10:05 +01:00
final wrap = Wrap(
runSpacing: 10.0,
crossAxisAlignment: WrapCrossAlignment.center,
children: yomiCardsWithTitle,
);
2021-12-01 23:09:53 +01:00
if (!isExpandable)
2022-01-19 02:10:05 +01:00
return wrap;
2021-12-01 23:09:53 +01:00
else
return ExpansionTile(
title: Center(
2022-01-19 02:10:05 +01:00
child: yomiCard(yomi: type.title, colors: type.getColors(context)),
2021-08-08 23:16:54 +02:00
),
2021-12-01 23:09:53 +01:00
children: [
2022-01-19 02:10:05 +01:00
const SizedBox(height: 20.0),
wrap,
const SizedBox(height: 25.0),
2021-12-01 23:09:53 +01:00
],
);
2021-08-08 23:16:54 +02:00
}
@override
Widget build(BuildContext context) {
return Container(
2021-12-01 23:09:53 +01:00
margin: const EdgeInsets.symmetric(
2021-08-08 23:16:54 +02:00
horizontal: 10.0,
2021-12-01 23:09:53 +01:00
vertical: 5.0,
2021-08-08 23:16:54 +02:00
),
2021-12-01 23:09:53 +01:00
alignment: Alignment.centerLeft,
child: yomiWrapper(context),
2021-08-08 23:16:54 +02:00
);
}
}