ModernMemoires/lib/views/write_page/write_page.dart

113 lines
3.7 KiB
Dart
Raw Normal View History

2023-12-17 23:00:31 +01:00
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:moody/views/write_page/widget/why_widget.dart';
2023-12-25 14:10:31 +01:00
import '../../utils/CirclePainter.dart';
import '../../utils/logic/PreferencesService.dart';
import '../../utils/widgets/QuestionSliderWidget.dart';
2023-12-17 23:00:31 +01:00
class WritePage extends StatefulWidget {
final double moodPercentage;
const WritePage({Key? key, required this.moodPercentage}) : super(key: key);
@override
_WritePageState createState() => _WritePageState();
}
class _WritePageState extends State<WritePage> {
double _sliderValue = 0.0;
bool _sliderChanged = true;
2023-12-25 14:10:31 +01:00
final PreferencesService _prefsService = PreferencesService();
TextEditingController _textController = TextEditingController(text: "why? ");
2023-12-17 23:00:31 +01:00
@override
void initState() {
super.initState();
_sliderValue = widget.moodPercentage; // Set the value here
}
2023-12-25 14:10:31 +01:00
void _saveEntry() async {
// Create a DiaryEntry object from the input
try {
List<String> texts =
_textController.text.isEmpty ? [] : [_textController.text];
DiaryEntry entry = DiaryEntry(
date: DateTime.now(), // or some date picker value
percentValue: _sliderValue.toInt(),
texts: texts,
);
// Save the entry using PreferencesService
await _prefsService.saveDiaryEntry(entry);
// Handle successful save here, maybe show a snackbar or navigate away
} catch (e) {
// Handle any errors here, such as invalid percent value or failed save
print(
"Error saving entry: $e"); // Consider showing an error dialog or toast instead
}
}
2023-12-17 23:00:31 +01:00
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Stack(
children: [
// Background circle
Positioned.fill(
child: CustomPaint(
painter: CirclePainter(_sliderValue),
),
),
// Main content
Padding(
2023-12-25 14:10:31 +01:00
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
2023-12-17 23:00:31 +01:00
// padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2023-12-25 14:10:31 +01:00
QuestionSliderWidget(
date: DateTime.now(),
questionText: "How are you today",
initialSliderValue: 0,
isSliderEnabled: true,
onSliderChanged: (value) {
setState(() {
_sliderValue = value;
});
}),
2023-12-17 23:00:31 +01:00
TextButton(
onPressed: () {
context.go('/write', extra: _sliderValue);
},
2023-12-25 14:10:31 +01:00
child: Padding(
padding: const EdgeInsets.fromLTRB(25, 0, 25, 25),
child: WhyWidget(controller: _textController),
),
2023-12-17 23:00:31 +01:00
),
],
),
),
// Skip/Save button
Positioned(
bottom: 20,
right: 20,
child: TextButton(
2023-12-18 17:51:22 +01:00
onPressed: () {
context.go("/home");
},
2023-12-17 23:00:31 +01:00
child: Text(_sliderChanged ? "Save" : "Skip"),
),
),
],
),
),
),
),
);
}
}