outsourced mood_form widgets

main
Julian Gegner 2023-02-15 23:28:19 +01:00
parent 465b4504a9
commit 5947e012c5
3 changed files with 55 additions and 25 deletions

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:smoke_cess_app/widgets/slider.dart'; import 'package:smoke_cess_app/widgets/slider.dart';
import 'package:smoke_cess_app/widgets/submit_form_button.dart';
import 'package:smoke_cess_app/widgets/text_formfield.dart';
class MoodForm extends StatefulWidget { class MoodForm extends StatefulWidget {
const MoodForm({super.key}); const MoodForm({super.key});
@ -13,6 +14,19 @@ class _MoodFormState extends State<MoodForm> {
final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>(); final GlobalKey<FormState> _moodFormKey = GlobalKey<FormState>();
MySlider slider = const MySlider(); MySlider slider = const MySlider();
String _textInput = ""; String _textInput = "";
void submitForm() {
if (_moodFormKey.currentState!.validate()) {
_moodFormKey.currentState?.save(); //call every onSave Method
//TODO Businesslogik aufrufen!
print(_textInput);
print(slider.getSliderValue());
_moodFormKey.currentState?.reset();
}
}
void onFormFieldSave(String? newValue) => _textInput = newValue!;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Form( return Form(
@ -21,30 +35,8 @@ class _MoodFormState extends State<MoodForm> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
slider, slider,
TextFormField( MyTextFormField(onFormFieldSave),
onSaved: (newValue) => _textInput = newValue!, SubmitFormButton(submitForm)
decoration:
const InputDecoration(hintText: 'Beschreibe deine Stimmung?'),
validator: (String? value) =>
value == null || value.isEmpty ? 'Text eingeben' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
if (_moodFormKey.currentState!.validate()) {
_moodFormKey.currentState
?.save(); //call every onSave Method
print(_textInput);
print(slider.getSliderValue());
_moodFormKey.currentState?.reset();
}
},
child: const Text('Speichern'),
),
),
], ],
)); ));
} }

View File

@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class SubmitFormButton extends StatelessWidget {
final VoidCallback submitCallback;
const SubmitFormButton(this.submitCallback, {super.key});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: submitCallback,
child: const Text('Speichern'),
),
);
}
}

View File

@ -0,0 +1,21 @@
import 'package:flutter/material.dart';
class MyTextFormField extends StatelessWidget {
final Function(String?) onSaveAction;
const MyTextFormField(
this.onSaveAction, {
super.key,
});
@override
Widget build(BuildContext context) {
return TextFormField(
onSaved: onSaveAction,
decoration: const InputDecoration(hintText: 'Beschreibe deine Stimmung?'),
validator: (String? value) =>
value == null || value.isEmpty ? 'Text eingeben' : null,
keyboardType: TextInputType.multiline,
maxLines: null,
);
}
}