yokutango-mobile-reader/lib/screens/practise/kanji.dart

152 lines
4.5 KiB
Dart
Raw Normal View History

2022-01-29 19:16:13 +01:00
import 'package:flutter/material.dart';
2024-04-26 01:14:49 +02:00
import 'package:flutter/services.dart';
2022-02-04 04:22:35 +01:00
import 'package:signature/signature.dart';
2022-01-29 19:16:13 +01:00
import '../../models/data_entry.dart';
2022-02-04 04:22:35 +01:00
import '../../components/navigation_buttons.dart';
2022-01-29 19:16:13 +01:00
class KanjiPage extends StatefulWidget {
final KanjiEntry entry;
final Function() onNextCard;
2022-02-02 23:45:37 +01:00
final Function() onPreviousCard;
final bool showDrawingPanel;
final bool showStrokeOrder;
2022-01-29 19:16:13 +01:00
final int? index;
const KanjiPage({
required this.entry,
required this.onNextCard,
2022-02-02 23:45:37 +01:00
required this.onPreviousCard,
this.showDrawingPanel = false,
this.showStrokeOrder = false,
2022-01-29 19:16:13 +01:00
this.index,
2024-04-26 01:14:49 +02:00
super.key,
});
2022-01-29 19:16:13 +01:00
@override
_KanjiPageState createState() => _KanjiPageState();
}
class _KanjiPageState extends State<KanjiPage> {
2024-04-26 01:14:49 +02:00
final focusNode = FocusNode();
2022-01-29 19:16:13 +01:00
bool isPressed = false;
2022-02-04 04:22:35 +01:00
late final controller = SignatureController(
2024-04-26 01:14:49 +02:00
penColor: Theme.of(context).textTheme.bodyMedium?.color ?? Colors.black,
2022-02-04 04:22:35 +01:00
);
2022-01-29 19:16:13 +01:00
2024-04-26 01:14:49 +02:00
@override
void initState() {
focusNode.requestFocus();
super.initState();
}
@override
void dispose() {
focusNode.dispose();
super.dispose();
}
2022-01-29 19:16:13 +01:00
@override
Widget build(BuildContext context) {
2024-04-26 01:14:49 +02:00
return RawKeyboardListener(
focusNode: focusNode,
onKey: (event) {
if (event.isKeyPressed(LogicalKeyboardKey.enter) || event.isKeyPressed(LogicalKeyboardKey.space)) {
if (widget.showDrawingPanel) return;
if (isPressed) {
controller.clear();
widget.onNextCard();
}
setState(() => isPressed = !isPressed);
2022-02-04 04:22:35 +01:00
}
2022-01-29 19:16:13 +01:00
},
2024-04-26 01:14:49 +02:00
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
if (widget.showDrawingPanel) return;
if (isPressed) {
controller.clear();
widget.onNextCard();
}
setState(() => isPressed = !isPressed);
},
child: Stack(
alignment: Alignment.center,
fit: StackFit.expand,
children: [
if (widget.showDrawingPanel) ...[
2022-02-04 04:22:35 +01:00
const SizedBox(width: 20),
2024-04-26 01:14:49 +02:00
Signature(
controller: controller,
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
)
2022-02-02 23:45:37 +01:00
],
2024-04-26 01:14:49 +02:00
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
widget.entry.kana.join('\n'),
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(width: 20),
const Divider(thickness: 5),
const SizedBox(width: 20),
Text(
widget.entry.kanji,
style: isPressed
? Theme.of(context).textTheme.headlineLarge
: Theme.of(context)
.textTheme
.headlineLarge
?.merge(const TextStyle(color: Colors.transparent)),
),
const SizedBox(width: 20),
const Divider(thickness: 5),
if (widget.showDrawingPanel) ...[
const SizedBox(width: 20),
Row(
children: [
const Expanded(child: SizedBox()),
IconButton(
iconSize: 40,
onPressed: controller.clear,
icon: const Icon(Icons.delete),
)
],
)
]
],
2022-02-02 23:45:37 +01:00
),
2024-04-26 01:14:49 +02:00
Positioned(
bottom: 40,
child: NavigationButtons(
middleText:
widget.index == null ? 'N' : (widget.index! + 1).toString(),
onMiddlePressed: () {
if (isPressed) {
controller.clear();
widget.onNextCard();
}
setState(() => isPressed = !isPressed);
},
onNextCard: () => setState(() {
if (isPressed) {
controller.clear();
widget.onNextCard();
}
setState(() => isPressed = !isPressed);
}),
onPreviousCard: () => setState(() {
controller.clear();
isPressed = false;
widget.onPreviousCard();
}),
),
)
],
),
2022-01-29 19:16:13 +01:00
),
);
}
}