Skip to content

Commit

Permalink
added Custom vocabs
Browse files Browse the repository at this point in the history
  • Loading branch information
HumanBot000 committed Aug 25, 2024
1 parent de26ef9 commit 59ce37b
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 229 deletions.
136 changes: 136 additions & 0 deletions lib/addCustomVocabulary.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import 'package:flutter/material.dart';
import 'package:vocabulingo/addTopic.dart';
import 'package:vocabulingo/learningSession.dart';
import 'package:vocabulingo/src/configuration.dart';

class AddCustomVocabulary extends StatefulWidget {
const AddCustomVocabulary({super.key});

@override
State<AddCustomVocabulary> createState() => _AddCustomVocabularyState();
}

class _AddCustomVocabularyState extends State<AddCustomVocabulary> {
late TextEditingController vocabularyController;
late TextEditingController translationController;

@override
void initState() {
super.initState();
vocabularyController = TextEditingController();
translationController = TextEditingController();
}

@override
void dispose() {
vocabularyController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: defaultAppBar(),
body: Stack(children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
"Add Custom Vocabulary",
style: TextStyle(fontSize: 30),
),
const Text(
"Please enter the vocabulary and the translations separated by a comma.",
style: TextStyle(fontSize: 20),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextFormField(
controller: vocabularyController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Vocabulary',
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: getActiveLanguageSVGPath(),
)
],
),
),
const Icon(
Icons.arrow_downward,
size: 100,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextFormField(
controller: translationController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Translation',
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: getSourceLanguageSVGPath(),
)
],
),
),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
WidgetStateProperty.all<Color>(Colors.green),
),
onPressed: () {
if (vocabularyController.text == "" ||
translationController.text == "") {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Error"),
content: const Text(
"Please add a vocabulary and a translation."),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("OK")),
]));
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddTopic(
vocabulary: [vocabularyController.text],
translation: translationController.text,
)));
}
},
child: const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Add"),
Padding(
padding: EdgeInsets.all(16.0),
child: Icon(
Icons.save,
color: Colors.black,
),
)
],
))
],
),
]));
}
}
187 changes: 116 additions & 71 deletions lib/addTopic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import 'package:flutter_svg/svg.dart';
import 'package:hive/hive.dart';
import 'package:provider/provider.dart';
import 'package:vocabulingo/home.dart';
import 'package:vocabulingo/learningSession.dart';
import 'package:vocabulingo/main.dart';
import 'package:vocabulingo/src/configuration.dart';
import 'package:vocabulingo/src/icons/my_flutter_app_icons.dart' as CustomIcons;
import 'package:http/http.dart' as http;
import 'dart:math' as math;

class AddTopic extends StatefulWidget {
const AddTopic({Key? key, required this.vocabulary, this.name})
final bool showCreated;
final String? translation;
const AddTopic(
{Key? key,
required this.vocabulary,
this.name,
this.showCreated = true,
this.translation})
: super(key: key);

final List<String> vocabulary;
Expand All @@ -25,7 +33,6 @@ class AddTopic extends StatefulWidget {
}

class _AddTopicState extends State<AddTopic> {

late TextEditingController textFieldController;
late IconData currentSelectedIcon;

Expand All @@ -42,60 +49,72 @@ class _AddTopicState extends State<AddTopic> {

List<Widget> _getTopicButtons() {
List<Widget> children = [];
for (String topic in getAllTopics()) {
children.add(Row(
children: [
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent.shade700),
onPressed: () {
setState(() {
if (vocabIsInTopic(
topic,
widget.vocabulary.toString().substring(
1, widget.vocabulary
.toString()
.length - 1))) {
showDialog(
context: context,
builder: (context) =>
AlertDialog(
title: const Text(
"This vocabulary is already in this topic"),
content: const Text(
"Do you want to remove it from that topic?"),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("Cancel")),
ElevatedButton(
onPressed: () {
removeVocabulariesFromTopic(topic,
[widget.vocabulary.toString()]);
if (topicIsEmpty(topic)) {
deleteTopic(topic);
}
Navigator.pop(context);
Navigator.pop(context);
},
child: const Text("Remove")),
]));
} else {
addVocabulariesToTopic(
topic, [widget.vocabulary.toString()]);
Navigator.pop(context);
}
});
},
child: Text(topic,
style: TextStyle(color: Colors.black, fontSize: 20)),
if (widget.showCreated) {
for (String topic in getAllTopics()) {
children.add(Row(
children: [
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.greenAccent.shade700),
onPressed: () {
setState(() {
if (vocabIsInTopic(
topic,
widget.vocabulary.toString().substring(
1, widget.vocabulary.toString().length - 1))) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text(
"This vocabulary is already in this topic"),
content: const Text(
"Do you want to remove it from that topic?"),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("Cancel")),
ElevatedButton(
onPressed: () {
removeVocabulariesFromTopic(topic,
[widget.vocabulary.toString()]);
if (topicIsEmpty(topic)) {
deleteTopic(topic);
}
Navigator.pop(context);
Navigator.pop(context);
},
child: const Text("Remove")),
]));
} else {
if (widget.translation == null) {
addVocabulariesToTopic(
topic, [widget.vocabulary.toString()]);
Navigator.pop(context);
} else {
addCustomVocabularyToTopic(topic, widget.vocabulary[0],
widget.translation.toString());
if (topic != "All") {
addCustomVocabularyToTopic(
"All",
widget.vocabulary[0],
widget.translation.toString());
}
Navigator.pop(context);
Navigator.pop(context);
}
}
});
},
child: Text(topic,
style: TextStyle(color: Colors.black, fontSize: 20)),
),
),
),
getTopicIcon(topic),
const Divider()
],
));
getTopicIcon(topic),
const Divider()
],
));
}
}
children.add(Container(
padding: const EdgeInsets.all(50.0),
Expand Down Expand Up @@ -141,34 +160,60 @@ class _AddTopicState extends State<AddTopic> {
if (topicExists(textFieldController.text)) {
showDialog(
context: context,
builder: (context) =>
AlertDialog(
builder: (context) => AlertDialog(
title: const Text("Error"),
content: const Text("Topic already exists"),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("Ok")),
ElevatedButton(onPressed: () {
addVocabulariesToTopic(
textFieldController.text, [widget.vocabulary
.toString()
]);
Navigator.pop(context);
Navigator.pop(context);
}, child: const Text("Add to Topic"))
ElevatedButton(
onPressed: () {
if (widget.translation == null) {
addVocabulariesToTopic(
textFieldController.text,
[widget.vocabulary.toString()]);
Navigator.pop(context);
Navigator.pop(context);
} else {
addCustomVocabularyToTopic(
textFieldController.text,
widget.vocabulary[0],
widget.translation.toString());
if (textFieldController.text != "All") {
addCustomVocabularyToTopic(
"All",
widget.vocabulary[0],
widget.translation.toString());
}
Navigator.pop(context);
Navigator.pop(context);
Navigator.pop(context);
}
},
child: const Text("Add to Topic"))
]));
} else {
addTopic(textFieldController.text, currentSelectedIcon.codePoint);
addVocabulariesToTopic(
textFieldController.text, [widget.vocabulary.toString()]);
Navigator.pop(context);
if (widget.translation == null) {
addTopic(textFieldController.text, currentSelectedIcon.codePoint);
addVocabulariesToTopic(
textFieldController.text, [widget.vocabulary.toString()]);
Navigator.pop(context);
} else {
addCustomVocabularyToTopic(textFieldController.text,
widget.vocabulary[0], widget.translation.toString());
if (textFieldController.text != "All") {
addCustomVocabularyToTopic(
"All", widget.vocabulary[0], widget.translation.toString());
}
Navigator.pop(context);
Navigator.pop(context);
}
}
} else {
showDialog(
context: context,
builder: (context) =>
AlertDialog(
builder: (context) => AlertDialog(
title: const Text("Error"),
content: const Text("Please enter a topic name"),
actions: [
Expand Down
Loading

0 comments on commit 59ce37b

Please sign in to comment.