refactor slider to use provider
parent
8230a66df1
commit
c45b38a1df
|
@ -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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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: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<MoodForm> {
|
|||
MySlider slider = MySlider();
|
||||
String _textInput = "";
|
||||
|
||||
void submitForm() {
|
||||
void submitForm(BuildContext context) {
|
||||
var inputModel = context.watch<InputProvider>();
|
||||
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<MoodForm> {
|
|||
child:
|
||||
MyTextFormField('Beschreibe deine Stimmung', onFormFieldSave),
|
||||
),
|
||||
SubmitFormButton(submitForm)
|
||||
SubmitFormButton(() => submitForm(context))
|
||||
],
|
||||
));
|
||||
}
|
||||
|
|
|
@ -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<SleepForm> {
|
|||
const TimeOfDay(hour: 8, minute: 00),
|
||||
);
|
||||
|
||||
void submitForm() {
|
||||
void submitForm(BuildContext context) {
|
||||
var inputModel = context.watch<InputProvider>();
|
||||
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<SleepForm> {
|
|||
child: SizedBox(
|
||||
width: 140,
|
||||
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: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<StatefulWidget> createState() => SliderState();
|
||||
|
||||
double get sliderValue => _currentSliderValue;
|
||||
}
|
||||
|
||||
class SliderState extends State<MySlider> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var inputModel = context.watch<InputProvider>();
|
||||
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<MySlider> {
|
|||
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;
|
||||
},
|
||||
),
|
||||
],
|
||||
|
|
14
pubspec.lock
14
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:
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue