fnotes/lib/notes.dart

126 lines
3.5 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/theme_bloc.dart';
2024-03-17 17:02:00 +00:00
import 'package:uuid/uuid.dart';
2024-03-17 13:15:50 +00:00
class Notes extends StatefulWidget {
const Notes({
super.key,
});
@override
State<Notes> createState() => _NotesState();
}
class _NotesState extends State<Notes> {
List<String> notes = List.empty(growable: true);
2024-03-17 14:23:09 +00:00
final phraseController = TextEditingController();
2024-03-17 13:15:50 +00:00
2024-03-17 17:02:00 +00:00
Map<Uuid, String> notesMap = {};
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<ThemeBloc>().add(ThemeEvent());
},
)
],
),
body: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state) {
return ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
return Dismissible(
onDismissed: (direction) {
2024-03-17 14:23:09 +00:00
notes.removeAt(index);
setState(() {});
2024-03-17 13:15:50 +00:00
},
key: ValueKey(notes[index]),
child: Card(
elevation: 5,
color: state.theme.colorScheme.primaryContainer,
child: SizedBox(
height: 50,
child: Padding(
padding: const EdgeInsets.only(left: 20),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
notes[index],
style: TextStyle(
color:
state.theme.colorScheme.onPrimaryContainer),
),
),
),
),
),
);
},
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
2024-03-17 14:23:09 +00:00
final newNote = showEnterItemDialog(context);
newNote.then(
(value) {
if (value != null && value.isNotEmpty) {
addNote(value);
setState(() {});
}
},
);
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);
},
),
],
);
},
);
}
void addNote(String text) {
notes.add(text);
2024-03-17 13:15:50 +00:00
}
}