add custom srs
This commit is contained in:
120
lib/src/screens/add_card_screen.dart
Normal file
120
lib/src/screens/add_card_screen.dart
Normal file
@@ -0,0 +1,120 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:kana_kit/kana_kit.dart';
|
||||
import '../models/custom_kanji_item.dart';
|
||||
import '../services/custom_deck_repository.dart';
|
||||
|
||||
class AddCardScreen extends StatefulWidget {
|
||||
const AddCardScreen({super.key});
|
||||
|
||||
@override
|
||||
State<AddCardScreen> createState() => _AddCardScreenState();
|
||||
}
|
||||
|
||||
class _AddCardScreenState extends State<AddCardScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _japaneseController = TextEditingController();
|
||||
final _englishController = TextEditingController();
|
||||
final _kanjiController = TextEditingController();
|
||||
final _kanaKit = const KanaKit();
|
||||
final _deckRepository = CustomDeckRepository();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_japaneseController.addListener(_convertToKana);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_japaneseController.removeListener(_convertToKana);
|
||||
_japaneseController.dispose();
|
||||
_englishController.dispose();
|
||||
_kanjiController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _convertToKana() {
|
||||
final text = _japaneseController.text;
|
||||
final converted = _kanaKit.toKana(text);
|
||||
if (text != converted) {
|
||||
_japaneseController.value = _japaneseController.value.copyWith(
|
||||
text: converted,
|
||||
selection: TextSelection.fromPosition(
|
||||
TextPosition(offset: converted.length),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _saveCard() {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final newItem = CustomKanjiItem(
|
||||
characters: _japaneseController.text,
|
||||
meaning: _englishController.text,
|
||||
kanji: _kanjiController.text.isNotEmpty ? _kanjiController.text : null,
|
||||
);
|
||||
_deckRepository.addCard(newItem);
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Add New Card'),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _japaneseController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Japanese (Kana)',
|
||||
hintText: 'Enter Japanese vocabulary or kanji',
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter a Japanese term';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _kanjiController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Japanese (Kanji)',
|
||||
hintText: 'Enter the kanji (optional)',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextFormField(
|
||||
controller: _englishController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'English',
|
||||
hintText: 'Enter the English meaning',
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return 'Please enter an English meaning';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
ElevatedButton(
|
||||
onPressed: _saveCard,
|
||||
child: const Text('Save Card'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user