ModernMemoires/lib/views/write_page/write_page.dart

130 lines
4.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:moody/utils/widgets/WhyWidget.dart';
import '../../utils/CirclePainter.dart';
import '../../utils/definitions/ColorPairs.dart';
import '../../utils/definitions/style_guide.dart';
import '../../utils/logic/PreferencesService.dart';
import '../../utils/widgets/QuestionSliderWidget.dart';
class WritePage extends StatefulWidget {
final SliderChangeData sliderData;
const WritePage({Key? key, required this.sliderData}) : super(key: key);
@override
_WritePageState createState() => _WritePageState();
}
class _WritePageState extends State<WritePage> {
SliderChangeData _sliderData = SliderChangeData(0, 0);
final bool _sliderChanged = true;
final PreferencesService _prefsService = PreferencesService();
final TextEditingController _textController = TextEditingController(text: "warum? ");
Color backgroundColor = Colors.lightGreenAccent;
Color textColor = Colors.black;
@override
void initState() {
super.initState();
_sliderData = widget.sliderData; // Set the value here
_loadColor();
}
void _loadColor() async {
ColorPair colorPair = await PreferencesService().loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
}
void _saveEntry() async {
// Create a DiaryEntry object from the input
try {
List<String> texts = _textController.text.isEmpty ? [] : [_textController.text];
print("DerText:${_textController.text}");
DiaryEntry entry = DiaryEntry(
date: DateTime.now(), // or some date picker value
percentValue: _sliderData.value.toInt(),
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
print("Error saving entry: $e"); // Consider showing an error dialog or toast instead
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: AppStyle.backgroundColor,
body: SafeArea(
child: Stack(
children: [
// Background circle
Positioned.fill(
top: 15,
left: _sliderData.position - 40,
child: CustomPaint(
painter: CirclePainter(_sliderData.value, backgroundColor),
),
),
// Main content
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
// 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),
),
),
],
),
),
// 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)),
),
),
],
),
),
),
);
}
}