refactor slider to use provider
parent
8230a66df1
commit
c45b38a1df
|
@ -1,4 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
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';
|
import 'package:smoke_cess_app/widgets/mood_form.dart';
|
||||||
|
|
||||||
class MoodPage extends StatelessWidget {
|
class MoodPage extends StatelessWidget {
|
||||||
|
@ -6,8 +8,10 @@ class MoodPage extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const Center(
|
return Center(
|
||||||
child: MoodForm(),
|
child: ChangeNotifierProvider(
|
||||||
);
|
create: (context) => InputProvider(),
|
||||||
|
child: const MoodForm(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
import 'package:flutter/material.dart';
|
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';
|
import 'package:smoke_cess_app/widgets/sleep_form.dart';
|
||||||
|
|
||||||
class SleepPage extends StatelessWidget {
|
class SleepPage extends StatelessWidget {
|
||||||
|
@ -6,6 +8,10 @@ class SleepPage extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const Center(child: SleepForm());
|
return Center(
|
||||||
|
child: ChangeNotifierProvider(
|
||||||
|
create: (context) => InputProvider(),
|
||||||
|
child: const SleepForm(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,12 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:smoke_cess_app/models/mood.dart';
|
import 'package:smoke_cess_app/models/mood.dart';
|
||||||
import 'package:smoke_cess_app/services/database_service.dart';
|
import 'package:smoke_cess_app/services/database_service.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/submit_form_button.dart';
|
||||||
import 'package:smoke_cess_app/widgets/text_formfield.dart';
|
import 'package:smoke_cess_app/widgets/text_formfield.dart';
|
||||||
|
|
||||||
|
import '../providers/input_provider.dart';
|
||||||
import 'elevated_card.dart';
|
import 'elevated_card.dart';
|
||||||
|
|
||||||
class MoodForm extends StatefulWidget {
|
class MoodForm extends StatefulWidget {
|
||||||
|
@ -19,10 +21,13 @@ class _MoodFormState extends State<MoodForm> {
|
||||||
MySlider slider = MySlider();
|
MySlider slider = MySlider();
|
||||||
String _textInput = "";
|
String _textInput = "";
|
||||||
|
|
||||||
void submitForm() {
|
void submitForm(BuildContext context) {
|
||||||
|
var inputModel = context.watch<InputProvider>();
|
||||||
|
print(inputModel.sliderValue);
|
||||||
if (_moodFormKey.currentState!.validate()) {
|
if (_moodFormKey.currentState!.validate()) {
|
||||||
_moodFormKey.currentState?.save(); //call every onSave Method
|
_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);
|
DatabaseService.instance.addMood(mood);
|
||||||
_moodFormKey.currentState?.reset();
|
_moodFormKey.currentState?.reset();
|
||||||
}
|
}
|
||||||
|
@ -47,7 +52,7 @@ class _MoodFormState extends State<MoodForm> {
|
||||||
child:
|
child:
|
||||||
MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave),
|
MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave),
|
||||||
),
|
),
|
||||||
SubmitFormButton(submitForm)
|
SubmitFormButton(() => submitForm(context))
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:smoke_cess_app/models/sleep.dart';
|
import 'package:smoke_cess_app/models/sleep.dart';
|
||||||
import 'package:smoke_cess_app/services/database_service.dart';
|
import 'package:smoke_cess_app/services/database_service.dart';
|
||||||
import 'package:smoke_cess_app/widgets/elevated_card.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/text_formfield.dart';
|
||||||
import 'package:smoke_cess_app/widgets/timepicker.dart';
|
import 'package:smoke_cess_app/widgets/timepicker.dart';
|
||||||
|
|
||||||
|
import '../providers/input_provider.dart';
|
||||||
|
|
||||||
class SleepForm extends StatefulWidget {
|
class SleepForm extends StatefulWidget {
|
||||||
const SleepForm({Key? key}) : super(key: key);
|
const SleepForm({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@ -25,11 +28,12 @@ class _SleepFormState extends State<SleepForm> {
|
||||||
const TimeOfDay(hour: 8, minute: 00),
|
const TimeOfDay(hour: 8, minute: 00),
|
||||||
);
|
);
|
||||||
|
|
||||||
void submitForm() {
|
void submitForm(BuildContext context) {
|
||||||
|
var inputModel = context.watch<InputProvider>();
|
||||||
if (_sleepFormKey.currentState!.validate()) {
|
if (_sleepFormKey.currentState!.validate()) {
|
||||||
_sleepFormKey.currentState?.save(); //call every onSave Method
|
_sleepFormKey.currentState?.save(); //call every onSave Method
|
||||||
Sleep sleep = Sleep(
|
Sleep sleep = Sleep(
|
||||||
slider.sliderValue.toInt(),
|
inputModel.sliderValue.toInt(),
|
||||||
_textInput,
|
_textInput,
|
||||||
DateTime.now(),
|
DateTime.now(),
|
||||||
sleepTimePicker.getCurrentTime,
|
sleepTimePicker.getCurrentTime,
|
||||||
|
@ -84,7 +88,7 @@ class _SleepFormState extends State<SleepForm> {
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 140,
|
width: 140,
|
||||||
height: 80,
|
height: 80,
|
||||||
child: SubmitFormButton(submitForm),
|
child: SubmitFormButton(() => submitForm(context)),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,25 +1,19 @@
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:smoke_cess_app/providers/input_provider.dart';
|
||||||
|
|
||||||
// ignore: must_be_immutable
|
class MySlider extends StatelessWidget {
|
||||||
class MySlider extends StatefulWidget {
|
|
||||||
double _currentSliderValue = 50;
|
|
||||||
MySlider({Key? key}) : super(key: key);
|
MySlider({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
|
||||||
State<StatefulWidget> createState() => SliderState();
|
|
||||||
|
|
||||||
double get sliderValue => _currentSliderValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
class SliderState extends State<MySlider> {
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
var inputModel = context.watch<InputProvider>();
|
||||||
return Center(
|
return Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text('${widget._currentSliderValue.toInt()}',
|
Text('${inputModel.sliderValue.toInt()}',
|
||||||
style: const TextStyle(fontSize: 22)),
|
style: const TextStyle(fontSize: 22)),
|
||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
@ -27,31 +21,25 @@ class SliderState extends State<MySlider> {
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.remove_outlined),
|
icon: const Icon(Icons.remove_outlined),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
inputModel.sliderValue -= 1;
|
||||||
widget._currentSliderValue -= 1;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Slider(
|
child: Slider(
|
||||||
value: widget._currentSliderValue,
|
value: inputModel.sliderValue,
|
||||||
min: 0,
|
min: 0,
|
||||||
max: 100,
|
max: 100,
|
||||||
divisions: 100,
|
divisions: 100,
|
||||||
label: widget._currentSliderValue.round().toString(),
|
label: '${inputModel.sliderValue.toInt()}',
|
||||||
onChanged: (double value) {
|
onChanged: (double value) {
|
||||||
setState(() {
|
inputModel.sliderValue = value;
|
||||||
widget._currentSliderValue = value;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.add_outlined),
|
icon: const Icon(Icons.add_outlined),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
setState(() {
|
inputModel.sliderValue += 1;
|
||||||
widget._currentSliderValue += 1;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
14
pubspec.lock
14
pubspec.lock
|
@ -233,6 +233,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.0"
|
version: "3.0.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
path:
|
path:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -310,6 +317,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.2.4"
|
version: "4.2.4"
|
||||||
|
provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "6.0.5"
|
||||||
shared_preferences:
|
shared_preferences:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|
|
@ -34,6 +34,7 @@ dependencies:
|
||||||
sqflite:
|
sqflite:
|
||||||
path:
|
path:
|
||||||
path_provider: ^2.0.12
|
path_provider: ^2.0.12
|
||||||
|
provider: ^6.0.5
|
||||||
|
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
|
|
Loading…
Reference in New Issue