From 40018431dd81f7497474c2b35f23867a8a11de04 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Wed, 15 Feb 2023 14:30:25 +0100 Subject: [PATCH 1/4] make appbar title change dynamically --- lib/pages/main_page.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 48859bd..3241a4c 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -14,6 +14,13 @@ class MyHomePage extends StatefulWidget { class MyHomePageState extends State { int _selectedIndex = 2; + final List _titles = [ + 'Stimmung', + 'Schlaf', + 'Timer', + 'Rückfall', + 'Einstellungen' + ]; static const List _widgetOptions = [ MoodPage(), SleepPage(), @@ -29,7 +36,7 @@ class MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar(title: const Text('ZI Rauchentwöhnung')), + appBar: AppBar(title: Text(_titles[_selectedIndex])), body: _widgetOptions.elementAt(_selectedIndex), bottomNavigationBar: NavigationBar( onDestinationSelected: _onItemTapped, From 92333d7b2d772e05bfaa3a4408a9e9669ec634db Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Wed, 15 Feb 2023 14:30:40 +0100 Subject: [PATCH 2/4] added reusable slider, used it in mood page --- lib/pages/mood_page.dart | 3 ++- lib/widgets/slider.dart | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 lib/widgets/slider.dart diff --git a/lib/pages/mood_page.dart b/lib/pages/mood_page.dart index a8c4a4e..0e76795 100644 --- a/lib/pages/mood_page.dart +++ b/lib/pages/mood_page.dart @@ -1,10 +1,11 @@ import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/widgets/slider.dart'; class MoodPage extends StatelessWidget { const MoodPage({super.key}); @override Widget build(BuildContext context) { - return const Center(child: Text('Hier Fragen wir die Stimmung ab')); + return Column(children: const [MySlider(), Text('Freitext')]); } } diff --git a/lib/widgets/slider.dart b/lib/widgets/slider.dart new file mode 100644 index 0000000..2d800e2 --- /dev/null +++ b/lib/widgets/slider.dart @@ -0,0 +1,27 @@ +import 'package:flutter/material.dart'; + +class MySlider extends StatefulWidget { + const MySlider({super.key}); + + @override + State createState() => SliderState(); +} + +class SliderState extends State { + double _currentSliderValue = 50; + + @override + Widget build(BuildContext context) { + return Row( + children: [ + Slider( + value: _currentSliderValue, + max: 100, + label: _currentSliderValue.round().toString(), + onChanged: (double value) => + {setState((() => _currentSliderValue = value))}), + Text(_currentSliderValue.round().toString()) + ], + ); + } +} From 465b4504a92a69d1501e24b8034937feb1ef0f81 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Wed, 15 Feb 2023 23:02:29 +0100 Subject: [PATCH 3/4] implemented form for mood --- lib/pages/mood_page.dart | 6 +++-- lib/widgets/mood_form.dart | 51 ++++++++++++++++++++++++++++++++++++++ lib/widgets/slider.dart | 30 ++++++++++++++-------- 3 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 lib/widgets/mood_form.dart diff --git a/lib/pages/mood_page.dart b/lib/pages/mood_page.dart index 0e76795..bd1b36b 100644 --- a/lib/pages/mood_page.dart +++ b/lib/pages/mood_page.dart @@ -1,11 +1,13 @@ import 'package:flutter/material.dart'; -import 'package:smoke_cess_app/widgets/slider.dart'; +import 'package:smoke_cess_app/widgets/mood_form.dart'; class MoodPage extends StatelessWidget { const MoodPage({super.key}); @override Widget build(BuildContext context) { - return Column(children: const [MySlider(), Text('Freitext')]); + return const Center( + child: MoodForm(), + ); } } diff --git a/lib/widgets/mood_form.dart b/lib/widgets/mood_form.dart new file mode 100644 index 0000000..23264e9 --- /dev/null +++ b/lib/widgets/mood_form.dart @@ -0,0 +1,51 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:smoke_cess_app/widgets/slider.dart'; + +class MoodForm extends StatefulWidget { + const MoodForm({super.key}); + + @override + State createState() => _MoodFormState(); +} + +class _MoodFormState extends State { + final GlobalKey _moodFormKey = GlobalKey(); + MySlider slider = const MySlider(); + String _textInput = ""; + @override + Widget build(BuildContext context) { + return Form( + key: _moodFormKey, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + slider, + TextFormField( + onSaved: (newValue) => _textInput = newValue!, + 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'), + ), + ), + ], + )); + } +} diff --git a/lib/widgets/slider.dart b/lib/widgets/slider.dart index 2d800e2..fb8e063 100644 --- a/lib/widgets/slider.dart +++ b/lib/widgets/slider.dart @@ -1,26 +1,36 @@ import 'package:flutter/material.dart'; +double _currentSliderValue = 50; + class MySlider extends StatefulWidget { const MySlider({super.key}); @override State createState() => SliderState(); + + double getSliderValue() { + return _currentSliderValue; + } } class SliderState extends State { - double _currentSliderValue = 50; - @override Widget build(BuildContext context) { - return Row( + return Column( children: [ - Slider( - value: _currentSliderValue, - max: 100, - label: _currentSliderValue.round().toString(), - onChanged: (double value) => - {setState((() => _currentSliderValue = value))}), - Text(_currentSliderValue.round().toString()) + const Text('Bewerte deine Stimmung'), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Slider( + value: _currentSliderValue, + max: 100, + label: _currentSliderValue.round().toString(), + onChanged: (double value) => + {setState((() => _currentSliderValue = value))}), + Text(_currentSliderValue.round().toString()) + ], + ) ], ); } From 5947e012c5f273343b8c39bdfded3816abd5e58d Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Wed, 15 Feb 2023 23:28:19 +0100 Subject: [PATCH 4/4] outsourced mood_form widgets --- lib/widgets/mood_form.dart | 42 ++++++++++++----------------- lib/widgets/submit_form_button.dart | 17 ++++++++++++ lib/widgets/text_formfield.dart | 21 +++++++++++++++ 3 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 lib/widgets/submit_form_button.dart create mode 100644 lib/widgets/text_formfield.dart diff --git a/lib/widgets/mood_form.dart b/lib/widgets/mood_form.dart index 23264e9..fa7d175 100644 --- a/lib/widgets/mood_form.dart +++ b/lib/widgets/mood_form.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.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 { const MoodForm({super.key}); @@ -13,6 +14,19 @@ class _MoodFormState extends State { final GlobalKey _moodFormKey = GlobalKey(); MySlider slider = const MySlider(); 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 Widget build(BuildContext context) { return Form( @@ -21,30 +35,8 @@ class _MoodFormState extends State { mainAxisAlignment: MainAxisAlignment.center, children: [ slider, - TextFormField( - onSaved: (newValue) => _textInput = newValue!, - 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'), - ), - ), + MyTextFormField(onFormFieldSave), + SubmitFormButton(submitForm) ], )); } diff --git a/lib/widgets/submit_form_button.dart b/lib/widgets/submit_form_button.dart new file mode 100644 index 0000000..58f28c8 --- /dev/null +++ b/lib/widgets/submit_form_button.dart @@ -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'), + ), + ); + } +} diff --git a/lib/widgets/text_formfield.dart b/lib/widgets/text_formfield.dart new file mode 100644 index 0000000..b8a7d02 --- /dev/null +++ b/lib/widgets/text_formfield.dart @@ -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, + ); + } +}