ModernMemoires/lib/views/entry_view/entry_page.dart

153 lines
5.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import '../../utils/CirclePainter.dart';
import '../../utils/definitions/ColorPairs.dart';
import '../../utils/definitions/style_guide.dart';
import '../../utils/logic/PreferencesService.dart';
import '../../utils/widgets/BottomNavigationWidget.dart';
import '../../utils/widgets/MoodTextArea.dart';
import '../../utils/widgets/QuestionSliderWidget.dart';
class EntryPage extends StatefulWidget {
final DateTime date;
EntryPage({Key? key, required this.date}) : super(key: key);
@override
_EntryPageState createState() => _EntryPageState();
}
class _EntryPageState extends State<EntryPage> {
SliderChangeData sliderData = SliderChangeData(0, 0);
String _moodText = "";
final PreferencesService _prefsService = PreferencesService();
Color backgroundColor = Colors.lightGreenAccent;
Color textColor = Colors.black;
// Add these new class member variables
bool _isEditing = false;
final TextEditingController _textController = TextEditingController();
@override
void initState() {
super.initState();
_loadCurrentDiaryEntry();
_loadColor();
}
void _loadColor() async {
ColorPair colorPair = await _prefsService.loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
}
void _loadCurrentDiaryEntry() async {
DiaryEntry? currentEntry = await _prefsService.getDiaryEntryByDate(widget.date);
if (currentEntry != null) {
setState(() {
sliderData.value = currentEntry.percentValue.toDouble();
_moodText = currentEntry.texts.join(" "); // Concatenate all texts
});
} else {
setState(() {
sliderData.value = 0.0;
_moodText = "No text found for this date.";
});
}
// _textController.text = _moodText;
_textController.text = currentEntry != null ? currentEntry!.texts.join(" ") : _moodText;
}
void _toggleEdit() {
setState(() {
_isEditing = !_isEditing;
});
if (!_isEditing) {
_saveEntry();
}
}
void _saveEntry() async {
print("saveEntry....");
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppStyle.backgroundColor,
body: SafeArea(
child: SingleChildScrollView(
child: Stack(
children: [
// Background circle
Padding(
padding: const EdgeInsets.only(top: 250),
child: Positioned.fill(
child: CustomPaint(
painter: CirclePainter(sliderData.value, backgroundColor),
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
context.go("/statistic");
},
child: const Padding(
padding: EdgeInsets.only(left: 25, top: 80),
child: Row(children: [
Icon(size: 25, Icons.chevron_left, color: Colors.black), // "<" icon
SizedBox(
height: 140,
)
]),
),
),
Padding(
padding: const EdgeInsets.only(left: 30),
child: Column(
children: [
Text(
sliderData.value.toString() + "%",
style: TextStyle(color: Colors.black),
),
Text(DateFormat('dd MM yyyy').format(widget.date).toString(),
style: TextStyle(color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold)),
],
),
),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: MoodTextAreaWidget(
initialText: _moodText,
isDisabled: !_isEditing,
hasPrefix: false,
autoFocus: _isEditing,
forceBlinkingCursor: _isEditing,
)),
TextButton(
onPressed: _toggleEdit,
child: Text(
_isEditing ? "Save" : "Edit",
style: TextStyle(fontSize: 18, color: textColor), // Use appropriate styling
),
),
SizedBox(height: 400), // Space for floating bottom navigation bar
],
),
],
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: CustomBottomNavigationBar(initialSelectedIndex: 0),
);
}
}