diff --git a/lib/pages/mood_page.dart b/lib/pages/mood_page.dart index bd1b36b..546c6f1 100644 --- a/lib/pages/mood_page.dart +++ b/lib/pages/mood_page.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; import 'package:smoke_cess_app/widgets/mood_form.dart'; class MoodPage extends StatelessWidget { @@ -6,8 +8,10 @@ class MoodPage extends StatelessWidget { @override Widget build(BuildContext context) { - return const Center( - child: MoodForm(), - ); + return Center( + child: ChangeNotifierProvider( + create: (context) => InputProvider(), + child: const MoodForm(), + )); } } diff --git a/lib/pages/sleep_page.dart b/lib/pages/sleep_page.dart index e736f44..22221f3 100644 --- a/lib/pages/sleep_page.dart +++ b/lib/pages/sleep_page.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; import 'package:smoke_cess_app/widgets/sleep_form.dart'; class SleepPage extends StatelessWidget { @@ -6,6 +8,10 @@ class SleepPage extends StatelessWidget { @override Widget build(BuildContext context) { - return const Center(child: SleepForm()); + return Center( + child: ChangeNotifierProvider( + create: (context) => InputProvider(), + child: const SleepForm(), + )); } } diff --git a/lib/providers/input_provider.dart b/lib/providers/input_provider.dart new file mode 100644 index 0000000..95bd423 --- /dev/null +++ b/lib/providers/input_provider.dart @@ -0,0 +1,12 @@ +import 'package:flutter/cupertino.dart'; + +class InputProvider extends ChangeNotifier { + double _sliderValue = 50; + + double get sliderValue => _sliderValue; + + set sliderValue(double newValue) { + _sliderValue = newValue; + notifyListeners(); + } +} diff --git a/lib/widgets/mood_form.dart b/lib/widgets/mood_form.dart index c859d3f..f52db8c 100644 --- a/lib/widgets/mood_form.dart +++ b/lib/widgets/mood_form.dart @@ -1,10 +1,12 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/services/database_service.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'; +import '../providers/input_provider.dart'; import 'elevated_card.dart'; class MoodForm extends StatefulWidget { @@ -19,10 +21,13 @@ class _MoodFormState extends State { MySlider slider = MySlider(); String _textInput = ""; - void submitForm() { + void submitForm(BuildContext context) { + var inputModel = context.watch(); + print(inputModel.sliderValue); if (_moodFormKey.currentState!.validate()) { _moodFormKey.currentState?.save(); //call every onSave Method - Mood mood = Mood(slider.sliderValue.toInt(), _textInput, DateTime.now()); + Mood mood = + Mood(inputModel.sliderValue.toInt(), _textInput, DateTime.now()); DatabaseService.instance.addMood(mood); _moodFormKey.currentState?.reset(); } @@ -47,7 +52,7 @@ class _MoodFormState extends State { child: MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave), ), - SubmitFormButton(submitForm) + SubmitFormButton(() => submitForm(context)) ], )); } diff --git a/lib/widgets/sleep_form.dart b/lib/widgets/sleep_form.dart index a36318e..d9955bc 100644 --- a/lib/widgets/sleep_form.dart +++ b/lib/widgets/sleep_form.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; import 'package:smoke_cess_app/models/sleep.dart'; import 'package:smoke_cess_app/services/database_service.dart'; import 'package:smoke_cess_app/widgets/elevated_card.dart'; @@ -7,6 +8,8 @@ import 'package:smoke_cess_app/widgets/submit_form_button.dart'; import 'package:smoke_cess_app/widgets/text_formfield.dart'; import 'package:smoke_cess_app/widgets/timepicker.dart'; +import '../providers/input_provider.dart'; + class SleepForm extends StatefulWidget { const SleepForm({Key? key}) : super(key: key); @@ -25,11 +28,12 @@ class _SleepFormState extends State { const TimeOfDay(hour: 8, minute: 00), ); - void submitForm() { + void submitForm(BuildContext context) { + var inputModel = context.watch(); if (_sleepFormKey.currentState!.validate()) { _sleepFormKey.currentState?.save(); //call every onSave Method Sleep sleep = Sleep( - slider.sliderValue.toInt(), + inputModel.sliderValue.toInt(), _textInput, DateTime.now(), sleepTimePicker.getCurrentTime, @@ -84,7 +88,7 @@ class _SleepFormState extends State { child: SizedBox( width: 140, height: 80, - child: SubmitFormButton(submitForm), + child: SubmitFormButton(() => submitForm(context)), ), ), ], diff --git a/lib/widgets/slider.dart b/lib/widgets/slider.dart index b642916..bf7c5b3 100644 --- a/lib/widgets/slider.dart +++ b/lib/widgets/slider.dart @@ -1,25 +1,19 @@ +import 'package:provider/provider.dart'; import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/providers/input_provider.dart'; -// ignore: must_be_immutable -class MySlider extends StatefulWidget { - double _currentSliderValue = 50; +class MySlider extends StatelessWidget { MySlider({Key? key}) : super(key: key); - @override - State createState() => SliderState(); - - double get sliderValue => _currentSliderValue; -} - -class SliderState extends State { @override Widget build(BuildContext context) { + var inputModel = context.watch(); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const SizedBox(height: 16), - Text('${widget._currentSliderValue.toInt()}', + Text('${inputModel.sliderValue.toInt()}', style: const TextStyle(fontSize: 22)), Row( mainAxisAlignment: MainAxisAlignment.center, @@ -27,31 +21,25 @@ class SliderState extends State { IconButton( icon: const Icon(Icons.remove_outlined), onPressed: () { - setState(() { - widget._currentSliderValue -= 1; - }); + inputModel.sliderValue -= 1; }, ), Expanded( child: Slider( - value: widget._currentSliderValue, + value: inputModel.sliderValue, min: 0, max: 100, divisions: 100, - label: widget._currentSliderValue.round().toString(), + label: '${inputModel.sliderValue.toInt()}', onChanged: (double value) { - setState(() { - widget._currentSliderValue = value; - }); + inputModel.sliderValue = value; }, ), ), IconButton( icon: const Icon(Icons.add_outlined), onPressed: () { - setState(() { - widget._currentSliderValue += 1; - }); + inputModel.sliderValue += 1; }, ), ], diff --git a/pubspec.lock b/pubspec.lock index 04c064a..b899dd9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -233,6 +233,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.0" + nested: + dependency: transitive + description: + name: nested + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" path: dependency: "direct main" description: @@ -310,6 +317,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.2.4" + provider: + dependency: "direct main" + description: + name: provider + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.5" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index c4a8789..9204ca9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,7 @@ dependencies: sqflite: path: path_provider: ^2.0.12 + provider: ^6.0.5 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.