ModernMemoires/lib/views/write_page/write_page.dart

132 lines
4.4 KiB
Dart
Raw Normal View History

2024-01-09 12:34:44 +01:00
import 'package:flutter/foundation.dart';
2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
2024-01-09 12:34:44 +01:00
import 'package:moody/utils/widgets/why_widget.dart';
2023-12-17 23:00:31 +01:00
2024-01-09 12:34:44 +01:00
import '../../utils/circle_painter.dart';
import '../../utils/definitions/color_pair.dart';
2024-01-08 19:34:48 +01:00
import '../../utils/definitions/style_guide.dart';
2024-01-09 12:34:44 +01:00
import '../../utils/logic/preferences_service.dart';
import '../../utils/widgets/question_slider_widget.dart';
2023-12-25 14:10:31 +01:00
2023-12-17 23:00:31 +01:00
class WritePage extends StatefulWidget {
2024-01-01 19:52:42 +01:00
final SliderChangeData sliderData;
2023-12-17 23:00:31 +01:00
2024-01-01 19:52:42 +01:00
const WritePage({Key? key, required this.sliderData}) : super(key: key);
2023-12-17 23:00:31 +01:00
@override
2024-01-09 12:34:44 +01:00
State<WritePage> createState() => _WritePage();
2023-12-17 23:00:31 +01:00
}
2024-01-09 12:34:44 +01:00
class _WritePage extends State<WritePage> {
2024-01-01 19:52:42 +01:00
SliderChangeData _sliderData = SliderChangeData(0, 0);
2024-01-08 19:34:48 +01:00
final bool _sliderChanged = true;
2023-12-25 14:10:31 +01:00
final PreferencesService _prefsService = PreferencesService();
2024-01-08 19:34:48 +01:00
final TextEditingController _textController = TextEditingController(text: "warum? ");
Color backgroundColor = Colors.lightGreenAccent;
Color textColor = Colors.black;
2023-12-25 14:10:31 +01:00
2023-12-17 23:00:31 +01:00
@override
void initState() {
super.initState();
2024-01-01 19:52:42 +01:00
_sliderData = widget.sliderData; // Set the value here
2024-01-08 19:34:48 +01:00
_loadColor();
}
void _loadColor() async {
ColorPair colorPair = await PreferencesService().loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
2023-12-17 23:00:31 +01:00
}
2023-12-25 14:10:31 +01:00
void _saveEntry() async {
// Create a DiaryEntry object from the input
try {
2024-01-08 19:34:48 +01:00
List<String> texts = _textController.text.isEmpty ? [] : [_textController.text];
2023-12-25 14:10:31 +01:00
DiaryEntry entry = DiaryEntry(
date: DateTime.now(), // or some date picker value
2024-01-01 19:52:42 +01:00
percentValue: _sliderData.value.toInt(),
2023-12-25 14:10:31 +01:00
texts: texts,
);
// Save the entry using PreferencesService
await _prefsService.saveDiaryEntry(entry);
// Handle successful save here, maybe show a snackbar or navigate away
} catch (e) {
// Handle any errors here, such as invalid percent value or failed save
2024-01-09 12:34:44 +01:00
if (kDebugMode) {
print("Error saving entry: $e");
} // Consider showing an error dialog or toast instead
2023-12-25 14:10:31 +01:00
}
}
2023-12-17 23:00:31 +01:00
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
2024-01-08 19:34:48 +01:00
backgroundColor: AppStyle.backgroundColor,
2023-12-17 23:00:31 +01:00
body: SafeArea(
2024-01-08 19:34:48 +01:00
child: Stack(
children: [
// Background circle
Positioned.fill(
top: 15,
left: _sliderData.position - 40,
child: CustomPaint(
painter: CirclePainter(_sliderData.value, backgroundColor),
2023-12-17 23:00:31 +01:00
),
2024-01-08 19:34:48 +01:00
),
// Main content
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
2023-12-17 23:00:31 +01:00
2024-01-08 19:34:48 +01:00
// padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
QuestionSliderWidget(
onSliderPositionChanged: (value) {},
sliderColor: textColor,
date: DateTime.now(),
questionText: "",
initialSliderValue: _sliderData.value,
isSliderEnabled: true,
onSliderChanged: (value) {
_sliderData = value;
setState(() {
// _sliderValue = value;
});
}),
TextButton(
onPressed: () {
context.go('/write', extra: _sliderData);
},
child: Padding(
padding: const EdgeInsets.fromLTRB(25, 0, 25, 25),
child: WhyWidget(controller: _textController),
2023-12-17 23:00:31 +01:00
),
2024-01-08 19:34:48 +01:00
),
],
2023-12-17 23:00:31 +01:00
),
2024-01-08 19:34:48 +01:00
),
// Skip/Save button
Positioned(
bottom: 20,
right: 20,
child: TextButton(
onPressed: () {
_saveEntry();
context.go("/home");
},
child: Text(_sliderChanged ? "Save" : "Skip", style: TextStyle(color: textColor, fontSize: 16)),
2023-12-17 23:00:31 +01:00
),
2024-01-08 19:34:48 +01:00
),
],
2023-12-17 23:00:31 +01:00
),
),
),
);
}
}