import 'package:flutter/material.dart'; import 'package:moody/utils/definitions/style_guide.dart'; import 'package:moody/views/first_page/first_page.dart'; import '../../utils/CirclePainter.dart'; import '../../utils/definitions/ColorPairs.dart'; import '../../utils/logic/PreferencesService.dart'; import '../../utils/widgets/BottomNavigationWidget.dart'; import '../../utils/widgets/QuestionSliderWidget.dart'; import '../../utils/widgets/WhyWidget.dart'; class HomePage extends StatefulWidget { const HomePage({super.key}); @override State createState() => _HomePageState(); } class _HomePageState extends State { SliderChangeData sliderData = SliderChangeData(0, 0); String _moodText = ""; bool _isTextAreaEditable = false; final PreferencesService _prefsService = PreferencesService(); Color backgroundColor = Colors.lightGreenAccent; Color textColor = Colors.black; bool noData = false; final TextEditingController _textController = TextEditingController(text: ""); @override void initState() { super.initState(); _loadCurrentDiaryEntry(); _loadColor(); } void _loadColor() async { ColorPair colorPair = await PreferencesService().loadColorPair(); setState(() { backgroundColor = colorPair.backgroundColor; textColor = colorPair.textColor; }); } void _loadCurrentDiaryEntry() async { DiaryEntry? currentEntry = await _prefsService.getDiaryEntryByCurrentDate(); if (currentEntry != null) { print(currentEntry.texts.join(" ")); setState(() { sliderData.value = currentEntry.percentValue.toDouble(); print(currentEntry.percentValue); _moodText = currentEntry.texts.join(" "); // Assuming you want to concatenate all texts _textController.text = currentEntry.texts.join(" "); }); setState(() {}); print(sliderData.value); } else { setState(() { noData = true; }); } } void _toggleTextAreaEditability() { setState(() { if (_isTextAreaEditable) { _saveEntry(); } _isTextAreaEditable = !_isTextAreaEditable; }); } void _saveEntry() async { try { List texts = _textController.text.isEmpty ? [] : [_textController.text]; print("DerText:${_textController.text}"); DiaryEntry entry = DiaryEntry( date: DateTime.now(), percentValue: sliderData.value.toInt(), texts: texts, ); await _prefsService.saveDiaryEntry(entry); } catch (e) { print("Error saving entry: $e"); } } @override Widget build(BuildContext context) { return noData ? Scaffold( body: const FirstPage(showSkipText: false), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButton: CustomBottomNavigationBar(initialSelectedIndex: 1), ) : Scaffold( backgroundColor: AppStyle.backgroundColor, body: SafeArea( child: SingleChildScrollView( child: Stack( children: [ // Background circle Positioned.fill( top: 25, left: sliderData.position, child: CustomPaint( painter: CirclePainter(sliderData.value, backgroundColor), ), ), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ QuestionSliderWidget( onSliderPositionChanged: (value) { print("sliderposchanged"); }, sliderColor: textColor, date: DateTime.now(), questionText: 'today', initialSliderValue: sliderData.value.toDouble(), isSliderEnabled: _isTextAreaEditable, onSliderChanged: (value) { setState(() { sliderData.value = value.value; }); }, ), Padding( padding: const EdgeInsets.only(left: 30.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ /*MoodTextAreaWidget( controller: _textController, initialText: _moodText, isDisabled: !_isTextAreaEditable, hasPrefix: false, autoFocus: _isTextAreaEditable, forceBlinkingCursor: _isTextAreaEditable, ),*/ _isTextAreaEditable ? WhyWidget( controller: _textController, prependWhy: false, ) : Column( children: [ const SizedBox(height: 10), Text(_textController.text, style: TextStyle(color: textColor, fontSize: 18)), ], ), const SizedBox(height: 20), GestureDetector( onTap: _toggleTextAreaEditability, child: Text( _isTextAreaEditable ? "- Save" : "+ Edit", style: TextStyle(color: textColor, fontSize: 18), ), ), const SizedBox(height: 600), ], ), ), ], ), ], ), ), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, floatingActionButton: CustomBottomNavigationBar(initialSelectedIndex: 1), ); } }