ModernMemoires/lib/views/first_page/first_page.dart

162 lines
5.9 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:moody/utils/widgets/question_slider_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';
class FirstPage extends StatefulWidget {
final bool showSkipText; // Add this line
const FirstPage({Key? key, this.showSkipText = true}) // Modify this line
: super(key: key);
@override
State<FirstPage> createState() => _FirstPage();
}
class _FirstPage extends State<FirstPage> {
SliderChangeData sliderData = SliderChangeData(0, 50);
bool _sliderChanged = false;
final PreferencesService _prefsService = PreferencesService();
Color backgroundColor = Colors.lightGreenAccent;
Color textColor = Colors.black;
@override
void initState() {
super.initState();
_checkExistingEntry();
_loadColor();
}
void _checkExistingEntry() async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
DiaryEntry? entry = await _prefsService.getDiaryEntryByCurrentDate();
if (entry != null && mounted) {
context.go('/home');
}
});
}
void _loadColor() async {
ColorPair colorPair = await PreferencesService().loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
}
void _saveEntry() async {
try {
List<String> texts = [];
DiaryEntry entry = DiaryEntry(
date: DateTime.now(), // or some date picker value
percentValue: sliderData.value.toInt(),
texts: texts,
);
await _prefsService.saveDiaryEntry(entry);
} catch (e) {
if (kDebugMode) {
print("Error saving entry: $e");
} // Consider showing an error dialog or toast instead
}
}
@override
Widget build(BuildContext context) {
double topPadding = MediaQuery.of(context).size.height / 5;
return backgroundColor == Colors.lightGreenAccent
? const CircularProgressIndicator() // Show loading indicator while color is null
: MaterialApp(
home: Scaffold(
backgroundColor: AppStyle.backgroundColor,
body: SafeArea(
child: Stack(
children: [
// Background circle
Positioned.fill(
top: topPadding,
left: sliderData.position,
child: CustomPaint(
painter: CirclePainter(sliderData.value, backgroundColor),
),
),
// Main content
Padding(
padding: EdgeInsets.only(top: topPadding), // Apply the top padding
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
// Date
Stack(
children: [
QuestionSliderWidget(
onSliderPositionChanged: (value) {
//print("slider Moved");
},
sliderColor: textColor,
date: DateTime.now(),
questionText: "",
initialSliderValue: 0,
isSliderEnabled: true,
onSliderChanged: (value) {
setState(() {
sliderData = value;
if (!_sliderChanged) _sliderChanged = true;
});
}),
],
),
// Why? button
_sliderChanged
? TextButton(
onPressed: () {
context.go('/write', extra: sliderData);
},
child: const Padding(
padding: EdgeInsets.only(left: 25),
child: Text(
"warum?",
style: TextStyle(color: Colors.black, fontSize: 18),
),
),
)
: const SizedBox.shrink(),
],
),
),
// Skip/Save button
Positioned(
bottom: 20,
right: 20,
child: TextButton(
onPressed: () {
if (_sliderChanged) {
_saveEntry();
context.go('/home', extra: sliderData.value);
} else {
context.go('/home', extra: 0);
}
},
child: _sliderChanged
? Text("save.", style: TextStyle(fontSize: 18, color: textColor))
: widget.showSkipText // Use the showSkipText parameter
? const Text("skip", style: TextStyle(fontSize: 18, color: Colors.grey))
: const SizedBox.shrink(),
),
),
],
),
),
),
);
}
}