import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:moody/views/write_page/widget/why_widget.dart'; import '../../utils/CirclePainter.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 { SliderChangeData _sliderData = SliderChangeData(0, 0); bool _sliderChanged = true; final PreferencesService _prefsService = PreferencesService(); TextEditingController _textController = TextEditingController(text: "why? "); @override void initState() { super.initState(); _sliderData = widget.sliderData; // Set the value here } 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 print( "Error saving entry: $e"); // Consider showing an error dialog or toast instead } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: SingleChildScrollView( child: Stack( children: [ // Background circle Positioned.fill( child: CustomPaint( painter: CirclePainter(_sliderData.value, Colors.yellowAccent), ), ), // 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) { print("sliderposchanged"); }, sliderColor: Colors.lightBlue, date: DateTime.now(), questionText: "How are you today", initialSliderValue: 0, isSliderEnabled: true, onSliderChanged: (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"), ), ), ], ), ), ), ), ); } }