ModernMemoires/lib/views/home_page/home_page.dart

196 lines
7.5 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:moody/utils/definitions/style_guide.dart';
import 'package:moody/views/first_page/first_page.dart';
import '../../utils/circle_painter.dart';
import '../../utils/definitions/color_pair.dart';
import '../../utils/logic/preferences_service.dart';
import '../../utils/widgets/custom_bottom_navigation_bar.dart';
import '../../utils/widgets/no_Glow_scroll_behavior.dart';
import '../../utils/widgets/question_slider_widget.dart';
import '../../utils/widgets/why_widget.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
SliderChangeData sliderData = SliderChangeData(0, 0);
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) {
setState(() {
sliderData.value = currentEntry.percentValue.toDouble();
_textController.text = currentEntry.texts.join(" ");
});
setState(() {});
} else {
setState(() {
noData = true;
});
}
}
void _toggleTextAreaEditability() {
setState(() {
if (_isTextAreaEditable) {
_saveEntry();
}
_isTextAreaEditable = !_isTextAreaEditable;
});
}
void _saveEntry() async {
try {
List<String> texts = _textController.text.isEmpty ? [] : [_textController.text];
DiaryEntry entry = DiaryEntry(
date: DateTime.now(),
percentValue: sliderData.value.toInt(),
texts: texts,
);
await _prefsService.saveDiaryEntry(entry);
} catch (e) {
if (kDebugMode) {
print("Error saving entry: $e");
}
}
}
@override
Widget build(BuildContext context) {
double topPadding = MediaQuery.of(context).size.height / 5;
return noData
? const Scaffold(
body: FirstPage(showSkipText: false),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: CustomBottomNavigationBar(initialSelectedIndex: 1),
)
: Scaffold(
backgroundColor: AppStyle.backgroundColor,
body: SafeArea(
child: ScrollConfiguration(
behavior: NoGlowScrollBehavior(),
child: SingleChildScrollView(
child: Stack(
children: [
// Background circle
Positioned.fill(
top: topPadding + 8,
left: sliderData.position + 40,
child: CustomPaint(
painter: CirclePainter(sliderData.value, backgroundColor),
),
),
GestureDetector(
onTap: () {
if (!_isTextAreaEditable) {
setState(() {
if (_isTextAreaEditable) {
_saveEntry();
}
_isTextAreaEditable = !_isTextAreaEditable;
});
}
},
child: Padding(
padding: EdgeInsets.only(top: topPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
QuestionSliderWidget(
onSliderPositionChanged: (value) {
//print("Slider moved");
},
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: _isTextAreaEditable
? Text(
"- Save",
style: TextStyle(color: textColor, fontSize: 18),
)
: const SizedBox(),
),
const SizedBox(height: 600),
],
),
),
],
),
),
),
],
),
),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: const CustomBottomNavigationBar(initialSelectedIndex: 1),
);
}
}