This commit is contained in:
Vandad Nahavandipoor 2022-01-05 07:54:07 +01:00
parent 94b65f952b
commit 5ca53087d4
9 changed files with 133 additions and 64 deletions

View File

@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:mynotes/utilities/dialogs/generic_dialog.dart';
Future<bool> showDeleteDialog(BuildContext context) {
return showGenericDialog<bool>(
context: context,
title: 'Delete',
content: 'Are you sure you want to delete this item?',
optionsBuilder: () => {
'Cancel': false,
'Yes': true,
},
).then(
(value) => value ?? false,
);
}

View File

@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:mynotes/utilities/dialogs/generic_dialog.dart';
Future<void> showErrorDialog(
BuildContext context,
String text,
) {
return showGenericDialog<void>(
context: context,
title: 'An error occurred',
content: text,
optionsBuilder: () => {
'OK': null,
},
);
}

View File

@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
typedef DialogOptionBuilder<T> = Map<String, T?> Function();
Future<T?> showGenericDialog<T>({
required BuildContext context,
required String title,
required String content,
required DialogOptionBuilder optionsBuilder,
}) {
final options = optionsBuilder();
return showDialog<T>(
context: context,
builder: (context) {
return AlertDialog(
title: Text(title),
content: Text(content),
actions: options.keys.map((optionTitle) {
final value = options[optionTitle];
return TextButton(
onPressed: () {
if (value != null) {
Navigator.of(context).pop(value);
} else {
Navigator.of(context).pop();
}
},
child: Text(optionTitle),
);
}).toList(),
);
},
);
}

View File

@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:mynotes/utilities/dialogs/generic_dialog.dart';
Future<bool> showLogOutDialog(BuildContext context) {
return showGenericDialog<bool>(
context: context,
title: 'Log out',
content: 'Are you sure you want to log out?',
optionsBuilder: () => {
'Cancel': false,
'Log out': true,
},
).then(
(value) => value ?? false,
);
}

View File

@ -1,24 +0,0 @@
import 'package:flutter/material.dart';
Future<void> showErrorDialog(
BuildContext context,
String text,
) {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('An error occurred'),
content: Text(text),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
),
],
);
},
);
}

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:mynotes/constants/routes.dart';
import 'package:mynotes/services/auth/auth_exceptions.dart';
import 'package:mynotes/services/auth/auth_service.dart';
import 'package:mynotes/utilities/show_error_dialog.dart';
import 'package:mynotes/utilities/dialogs/error_dialog.dart';
class LoginView extends StatefulWidget {
const LoginView({Key? key}) : super(key: key);

View File

@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:mynotes/services/crud/notes_service.dart';
import 'package:mynotes/utilities/dialogs/delete_dialog.dart';
typedef DeleteNoteCallback = void Function(DatabaseNote note);
class NotesListView extends StatelessWidget {
final List<DatabaseNote> notes;
final DeleteNoteCallback onDeleteNote;
const NotesListView({
Key? key,
required this.notes,
required this.onDeleteNote,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
final note = notes[index];
return ListTile(
title: Text(
note.text,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
trailing: IconButton(
onPressed: () async {
final shouldDelete = await showDeleteDialog(context);
if (shouldDelete) {
onDeleteNote(note);
}
},
icon: const Icon(Icons.delete),
),
);
},
);
}
}

View File

@ -3,6 +3,8 @@ import 'package:mynotes/constants/routes.dart';
import 'package:mynotes/enums/menu_action.dart';
import 'package:mynotes/services/auth/auth_service.dart';
import 'package:mynotes/services/crud/notes_service.dart';
import 'package:mynotes/utilities/dialogs/logout_dialog.dart';
import 'package:mynotes/views/notes/notes_list_view.dart';
class NotesView extends StatefulWidget {
const NotesView({Key? key}) : super(key: key);
@ -71,18 +73,10 @@ class _NotesViewState extends State<NotesView> {
case ConnectionState.active:
if (snapshot.hasData) {
final allNotes = snapshot.data as List<DatabaseNote>;
return ListView.builder(
itemCount: allNotes.length,
itemBuilder: (context, index) {
final note = allNotes[index];
return ListTile(
title: Text(
note.text,
maxLines: 1,
softWrap: true,
overflow: TextOverflow.ellipsis,
),
);
return NotesListView(
notes: allNotes,
onDeleteNote: (note) async {
await _notesService.deleteNote(id: note.id);
},
);
} else {
@ -101,29 +95,3 @@ class _NotesViewState extends State<NotesView> {
);
}
}
Future<bool> showLogOutDialog(BuildContext context) {
return showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Sign out'),
content: const Text('Are you sure you want to sign out?'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('Log out'),
),
],
);
},
).then((value) => value ?? false);
}

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:mynotes/constants/routes.dart';
import 'package:mynotes/services/auth/auth_exceptions.dart';
import 'package:mynotes/services/auth/auth_service.dart';
import 'package:mynotes/utilities/show_error_dialog.dart';
import 'package:mynotes/utilities/dialogs/error_dialog.dart';
class RegisterView extends StatefulWidget {
const RegisterView({Key? key}) : super(key: key);