Skip to content

Commit

Permalink
move notes to separate page
Browse files Browse the repository at this point in the history
  • Loading branch information
Sesu8642 committed Mar 14, 2024
1 parent 88f56f4 commit 03f5553
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 137 deletions.
3 changes: 3 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
"configurations": [
{
"name": "infusion_timer (debug mode)",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"flutterMode": "debug",
"args": ["--flavor", "ci"]
},
{
"name": "infusion_timer (profile mode)",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"flutterMode": "profile",
"args": ["--flavor", "ci"]
},
{
"name": "infusion_timer (release mode)",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"flutterMode": "release",
Expand Down
129 changes: 129 additions & 0 deletions lib/widgets/notes_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-License-Identifier: GPL-3.0-or-later

import 'dart:async';

import 'package:infusion_timer/persistence_service.dart';
import 'package:flutter/material.dart';
import 'package:infusion_timer/tea.dart';

class NotesPage extends StatefulWidget {
final Tea tea;

const NotesPage({Key? key, required this.tea}) : super(key: key);

@override
NotesPageState createState() => NotesPageState();
}

class NotesPageState extends State<NotesPage> {
final _formKey = GlobalKey<FormState>();
bool _hasUnsavedChanges = false;
Future<void> _onPopInvoked(bool didPop) async {
// confirm cancelling infusion if going back to collection page
if (didPop) {
return;
}
final int? action = await showDialog<int>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Save unsaved changes?'),
content: const Text('You have unsaved changes.'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context, 0);
},
child: const Text('Save and Close'),
),
TextButton(
onPressed: () => Navigator.pop(context, 1),
child: const Text('Discard and Close'),
),
TextButton(
onPressed: () => Navigator.pop(context, 2),
child: const Text('Cancel'),
),
],
);
},
);
switch (action) {
case 0:
_formKey.currentState!.save();
PersistenceService.updateTea(widget.tea);
if (context.mounted) {
Navigator.pop(context);
}
break;
case 1:
if (context.mounted) {
Navigator.pop(context);
}
break;
case 2:
// nothing to do
break;
}
}

@override
Widget build(BuildContext context) {
return PopScope(
canPop: !_hasUnsavedChanges,
onPopInvoked: _onPopInvoked,
child: Scaffold(
appBar: AppBar(
title: const Text("Notes"),
),
body: Form(
key: _formKey,
child: Column(
children: [
Expanded(
child: Card(
child: Column(
children: [
ListTile(
title: Center(child: Text(widget.tea.name!)),
),
Expanded(
child: TextFormField(
minLines: 15,
initialValue: widget.tea.detailedNotes,
maxLines: null,
// using collapsed to hide black line on the bottom
decoration: const InputDecoration.collapsed(
hintText: 'Enter your notes here',
),
onChanged: (value) {
setState(() => _hasUnsavedChanges = true);
},
onSaved: (value) {
widget.tea.detailedNotes = value;
},
),
),
const SizedBox(height: 10),
FilledButton(
onPressed: () {
_formKey.currentState!.save();
PersistenceService.updateTea(widget.tea);
Navigator.pop(context);
},
child: const Text(
'Save and Close',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
],
),
),
),
],
),
),
),
);
}
}
Loading

0 comments on commit 03f5553

Please sign in to comment.