import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:moody/utils/widgets/why_widget.dart'; import '../../utils/circle_painter.dart'; import '../../utils/definitions/color_pair.dart'; import '../../utils/definitions/style_guide.dart'; import '../../utils/logic/preferences_service.dart'; import '../../utils/widgets/question_slider_widget.dart'; class WritePage extends StatefulWidget { final SliderChangeData sliderData; const WritePage({Key? key, required this.sliderData}) : super(key: key); @override State createState() => _WritePage(); } class _WritePage extends State { 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 texts = _textController.text.isEmpty ? [] : [_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 if (kDebugMode) { 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: [ 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)), ), ), ], ), ), ), ); } }