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 {
|
2024-01-01 19:52:42 +01:00
|
|
|
final SliderChangeData sliderData;
|
2023-12-17 23:00:31 +01:00
|
|
|
|
2024-01-01 19:52:42 +01:00
|
|
|
const WritePage({Key? key, required this.sliderData}) : super(key: key);
|
2023-12-17 23:00:31 +01:00
|
|
|
@override
|
|
|
|
_WritePageState createState() => _WritePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _WritePageState extends State<WritePage> {
|
2024-01-01 19:52:42 +01:00
|
|
|
SliderChangeData _sliderData = SliderChangeData(0, 0);
|
2023-12-17 23:00:31 +01:00
|
|
|
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();
|
2024-01-01 19:52:42 +01:00
|
|
|
_sliderData = widget.sliderData; // Set the value here
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
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
|
2024-01-01 19:52:42 +01:00
|
|
|
percentValue: _sliderData.value.toInt(),
|
2023-12-25 14:10:31 +01:00
|
|
|
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(
|
2024-01-01 19:52:42 +01:00
|
|
|
painter:
|
|
|
|
CirclePainter(_sliderData.value, Colors.yellowAccent),
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
// 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(
|
2024-01-01 18:31:10 +01:00
|
|
|
onSliderPositionChanged: (value) {
|
|
|
|
print("sliderposchanged");
|
|
|
|
},
|
2024-01-01 19:52:42 +01:00
|
|
|
sliderColor: Colors.lightBlue,
|
2023-12-25 14:10:31 +01:00
|
|
|
date: DateTime.now(),
|
|
|
|
questionText: "How are you today",
|
|
|
|
initialSliderValue: 0,
|
|
|
|
isSliderEnabled: true,
|
|
|
|
onSliderChanged: (value) {
|
|
|
|
setState(() {
|
2024-01-01 18:31:10 +01:00
|
|
|
// _sliderValue = value;
|
2023-12-25 14:10:31 +01:00
|
|
|
});
|
|
|
|
}),
|
2023-12-17 23:00:31 +01:00
|
|
|
TextButton(
|
|
|
|
onPressed: () {
|
2024-01-01 19:52:42 +01:00
|
|
|
context.go('/write', extra: _sliderData);
|
2023-12-17 23:00:31 +01:00
|
|
|
},
|
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: () {
|
2024-01-01 19:52:42 +01:00
|
|
|
_saveEntry();
|
2023-12-18 17:51:22 +01:00
|
|
|
context.go("/home");
|
|
|
|
},
|
2023-12-17 23:00:31 +01:00
|
|
|
child: Text(_sliderChanged ? "Save" : "Skip"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|