ModernMemoires/lib/views/first_page/first_page.dart

162 lines
5.9 KiB
Dart
Raw Normal View History

2024-01-09 12:34:44 +01:00
import 'package:flutter/foundation.dart';
2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
2024-01-09 12:34:44 +01:00
import 'package:moody/utils/widgets/question_slider_widget.dart';
2023-12-25 14:10:31 +01:00
2024-01-09 12:34:44 +01:00
import '../../utils/circle_painter.dart';
import '../../utils/definitions/color_pair.dart';
2024-01-08 19:34:48 +01:00
import '../../utils/definitions/style_guide.dart';
2024-01-09 12:34:44 +01:00
import '../../utils/logic/preferences_service.dart';
2023-12-17 23:00:31 +01:00
class FirstPage extends StatefulWidget {
2024-01-08 19:34:48 +01:00
final bool showSkipText; // Add this line
const FirstPage({Key? key, this.showSkipText = true}) // Modify this line
: super(key: key);
2023-12-17 23:00:31 +01:00
@override
2024-01-09 12:34:44 +01:00
State<FirstPage> createState() => _FirstPage();
2023-12-17 23:00:31 +01:00
}
2024-01-09 12:34:44 +01:00
class _FirstPage extends State<FirstPage> {
2024-01-08 19:34:48 +01:00
SliderChangeData sliderData = SliderChangeData(0, 50);
2023-12-17 23:00:31 +01:00
bool _sliderChanged = false;
2024-01-08 19:34:48 +01:00
final PreferencesService _prefsService = PreferencesService();
2023-12-25 14:10:31 +01:00
2024-01-01 18:31:10 +01:00
Color backgroundColor = Colors.lightGreenAccent;
Color textColor = Colors.black;
@override
void initState() {
super.initState();
2024-01-08 19:34:48 +01:00
_checkExistingEntry();
2024-01-01 18:31:10 +01:00
_loadColor();
}
2024-01-08 19:34:48 +01:00
void _checkExistingEntry() async {
2024-01-09 12:34:44 +01:00
WidgetsBinding.instance.addPostFrameCallback((_) async {
2024-01-08 19:34:48 +01:00
DiaryEntry? entry = await _prefsService.getDiaryEntryByCurrentDate();
2024-01-09 12:34:44 +01:00
if (entry != null && mounted) {
2024-01-08 19:34:48 +01:00
context.go('/home');
}
});
}
2024-01-01 18:31:10 +01:00
void _loadColor() async {
ColorPair colorPair = await PreferencesService().loadColorPair();
setState(() {
backgroundColor = colorPair.backgroundColor;
textColor = colorPair.textColor;
});
}
2023-12-25 14:10:31 +01:00
void _saveEntry() async {
try {
List<String> texts = [];
DiaryEntry entry = DiaryEntry(
date: DateTime.now(), // or some date picker value
2024-01-01 19:52:42 +01:00
percentValue: sliderData.value.toInt(),
2023-12-25 14:10:31 +01:00
texts: texts,
);
await _prefsService.saveDiaryEntry(entry);
} catch (e) {
2024-01-09 12:34:44 +01:00
if (kDebugMode) {
print("Error saving entry: $e");
} // Consider showing an error dialog or toast instead
2023-12-25 14:10:31 +01:00
}
}
2023-12-17 23:00:31 +01:00
@override
Widget build(BuildContext context) {
2024-01-13 17:07:11 +01:00
double topPadding = MediaQuery.of(context).size.height / 5;
2024-01-01 18:31:10 +01:00
return backgroundColor == Colors.lightGreenAccent
2024-01-08 19:34:48 +01:00
? const CircularProgressIndicator() // Show loading indicator while color is null
2024-01-01 18:31:10 +01:00
: MaterialApp(
home: Scaffold(
2024-01-08 19:34:48 +01:00
backgroundColor: AppStyle.backgroundColor,
2024-01-01 18:31:10 +01:00
body: SafeArea(
child: Stack(
children: [
// Background circle
Positioned.fill(
2024-01-13 17:07:11 +01:00
top: topPadding,
left: sliderData.position,
2024-01-01 18:31:10 +01:00
child: CustomPaint(
2024-01-08 19:34:48 +01:00
painter: CirclePainter(sliderData.value, backgroundColor),
2024-01-01 18:31:10 +01:00
),
),
// Main content
2024-01-13 17:07:11 +01:00
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),
),
2024-01-01 19:52:42 +01:00
),
2024-01-13 17:07:11 +01:00
)
: const SizedBox.shrink(),
],
),
2024-01-01 18:31:10 +01:00
),
// 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);
}
},
2024-01-01 19:52:42 +01:00
child: _sliderChanged
2024-01-08 19:34:48 +01:00
? Text("save.", style: TextStyle(fontSize: 18, color: textColor))
: widget.showSkipText // Use the showSkipText parameter
? const Text("skip", style: TextStyle(fontSize: 18, color: Colors.grey))
2024-01-09 12:34:44 +01:00
: const SizedBox.shrink(),
2024-01-01 18:31:10 +01:00
),
),
],
2023-12-17 23:00:31 +01:00
),
),
2024-01-01 18:31:10 +01:00
),
);
2023-12-17 23:00:31 +01:00
}
}