fnotes/lib/notes_app.dart

121 lines
3.6 KiB
Dart
Raw Normal View History

2024-03-17 13:15:50 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fnotes/note.dart';
import 'package:fnotes/persistent_notes_bloc.dart';
2024-03-17 13:15:50 +00:00
import 'package:fnotes/theme_bloc.dart';
class NotesApp extends StatefulWidget {
const NotesApp({
2024-03-17 13:15:50 +00:00
super.key,
});
@override
State<NotesApp> createState() => _NotesAppState();
2024-03-17 13:15:50 +00:00
}
class _NotesAppState extends State<NotesApp> {
2024-03-17 14:23:09 +00:00
final phraseController = TextEditingController();
2024-03-17 13:15:50 +00:00
@override
void initState() {
context.read<PersistentNotesBloc>().add(LoadNotesEvent());
super.initState();
}
2024-03-17 17:02:00 +00:00
2024-03-17 13:15:50 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Your notes:'),
actions: [
IconButton(
icon: const Icon(Icons.light_mode),
onPressed: () {
context.read<PersistedThemeBloc>().add(ThemeChangedEvent());
2024-03-17 13:15:50 +00:00
},
)
],
),
2024-06-11 20:47:19 +00:00
body: BlocBuilder<PersistentNotesBloc, PersistentNotesState>(
builder: (context, notesState) {
return ListView.builder(
itemCount: notesState.notes.length,
itemBuilder: (context, index) {
return Dismissible(
onDismissed: (direction) {
context.read<PersistentNotesBloc>().add(NoteDismissed(
Note.withId(
id: notesState.notes[index].id,
content: notesState.notes[index].content)));
},
2024-06-11 20:47:19 +00:00
key: ValueKey(notesState.notes[index]),
child: Card(
elevation: 5,
child: SizedBox(
height: 50,
child: Padding(
padding: const EdgeInsets.only(left: 20),
child: Align(
alignment: Alignment.centerLeft,
child: Text(notesState.notes[index].content)),
),
),
),
);
2024-06-11 20:47:19 +00:00
},
);
}),
2024-03-17 13:15:50 +00:00
floatingActionButton: FloatingActionButton(
onPressed: () {
2024-03-17 14:23:09 +00:00
final newNote = showEnterItemDialog(context);
newNote.then(
(value) {
if (value != null && value.isNotEmpty) {
context
.read<PersistentNotesBloc>()
.add(NoteEntered(Note(content: value)));
2024-03-17 14:23:09 +00:00
}
},
);
2024-03-17 13:15:50 +00:00
},
child: const Icon(Icons.add)),
);
}
2024-03-17 14:23:09 +00:00
Future<String?> showEnterItemDialog(BuildContext context) {
return showDialog<String>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
2024-03-17 17:02:00 +00:00
title: const Text('New list item'),
2024-03-17 14:23:09 +00:00
content: TextField(
onSubmitted: (val) {
Navigator.pop(context, val);
phraseController.clear();
},
autofocus: true,
controller: phraseController,
decoration: InputDecoration(
hintText: 'Remember me!',
suffixIcon: IconButton(
onPressed: () {
Navigator.pop(context, phraseController.value.text);
phraseController.clear();
},
icon: const Icon(Icons.check),
)),
),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
],
);
},
);
}
2024-03-17 13:15:50 +00:00
}