import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:fnotes/note.dart'; import 'package:fnotes/persistent_notes_bloc.dart'; import 'package:fnotes/theme_bloc.dart'; class NotesApp extends StatefulWidget { const NotesApp({ super.key, }); @override State createState() => _NotesAppState(); } class _NotesAppState extends State { final phraseController = TextEditingController(); @override void initState() { context.read().add(LoadNotesEvent()); super.initState(); } @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().add(ThemeChangedEvent()); }, ) ], ), body: BlocBuilder( builder: (context, notesState) { return ListView.builder( itemCount: notesState.notes.length, itemBuilder: (context, index) { return Dismissible( onDismissed: (direction) { context.read().add(NoteDismissed( Note.withId( id: notesState.notes[index].id, content: notesState.notes[index].content))); }, 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)), ), ), ), ); }, ); }), floatingActionButton: FloatingActionButton( onPressed: () { final newNote = showEnterItemDialog(context); newNote.then( (value) { if (value != null && value.isNotEmpty) { context .read() .add(NoteEntered(Note(content: value))); } }, ); }, child: const Icon(Icons.add)), ); } Future showEnterItemDialog(BuildContext context) { return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('New list item'), 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: [ TextButton( child: const Text('Cancel'), onPressed: () { Navigator.pop(context); }, ), ], ); }, ); } }