|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:crud_app/models/note.dart'; |
| 3 | +import 'package:crud_app/db/database_helper.dart'; |
| 4 | +import 'login_page.dart'; |
| 5 | +import 'home_page.dart'; |
| 6 | +void main() { |
| 7 | + runApp(MyApp()); |
| 8 | +} |
| 9 | + |
| 10 | +class MyApp extends StatelessWidget { |
| 11 | + @override |
| 12 | + Widget build(BuildContext context) { |
| 13 | + return MaterialApp( |
| 14 | + title: 'CRUD App', |
| 15 | + theme: ThemeData( |
| 16 | + primarySwatch: Colors.blue, |
| 17 | + ), |
| 18 | + home: HomePage(), |
| 19 | + ); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class HomePage extends StatefulWidget { |
| 24 | + @override |
| 25 | + _HomePageState createState() => _HomePageState(); |
| 26 | +} |
| 27 | + |
| 28 | +class _HomePageState extends State<HomePage> { |
| 29 | + final DatabaseHelper _dbHelper = DatabaseHelper(); |
| 30 | + List<Note> _notes = []; |
| 31 | + bool _isLoading = true; |
| 32 | + |
| 33 | + @override |
| 34 | + void initState() { |
| 35 | + super.initState(); |
| 36 | + _refreshNotes(); |
| 37 | + } |
| 38 | + |
| 39 | + Future<void> _refreshNotes() async { |
| 40 | + final notes = await _dbHelper.getNotes(); |
| 41 | + setState(() { |
| 42 | + _notes = notes; |
| 43 | + _isLoading = false; |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + Future<void> _addOrEditNoteDialog([Note? note]) async { |
| 48 | + final titleController = TextEditingController(text: note?.title); |
| 49 | + final descriptionController = TextEditingController(text: note?.description); |
| 50 | + |
| 51 | + await showDialog( |
| 52 | + context: context, |
| 53 | + builder: (context) { |
| 54 | + return AlertDialog( |
| 55 | + title: Text(note == null ? 'Add Note' : 'Edit Note'), |
| 56 | + content: Column( |
| 57 | + mainAxisSize: MainAxisSize.min, |
| 58 | + children: [ |
| 59 | + TextField( |
| 60 | + controller: titleController, |
| 61 | + decoration: InputDecoration(labelText: 'Title'), |
| 62 | + ), |
| 63 | + TextField( |
| 64 | + controller: descriptionController, |
| 65 | + decoration: InputDecoration(labelText: 'Description'), |
| 66 | + ), |
| 67 | + ], |
| 68 | + ), |
| 69 | + actions: [ |
| 70 | + TextButton( |
| 71 | + onPressed: () => Navigator.of(context).pop(), |
| 72 | + child: Text('Cancel'), |
| 73 | + ), |
| 74 | + TextButton( |
| 75 | + onPressed: () async { |
| 76 | + final newNote = Note( |
| 77 | + id: note?.id, |
| 78 | + title: titleController.text, |
| 79 | + description: descriptionController.text, |
| 80 | + ); |
| 81 | + |
| 82 | + if (note == null) { |
| 83 | + await _dbHelper.insertNote(newNote); |
| 84 | + } else { |
| 85 | + await _dbHelper.updateNote(newNote); |
| 86 | + } |
| 87 | + |
| 88 | + _refreshNotes(); |
| 89 | + Navigator.of(context).pop(); |
| 90 | + }, |
| 91 | + child: Text('Save'), |
| 92 | + ), |
| 93 | + ], |
| 94 | + ); |
| 95 | + }, |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + Future<void> _deleteNoteDialog(int id) async { |
| 100 | + await showDialog( |
| 101 | + context: context, |
| 102 | + builder: (context) { |
| 103 | + return AlertDialog( |
| 104 | + title: Text('Delete Note'), |
| 105 | + content: Text('Are you sure you want to delete this note?'), |
| 106 | + actions: [ |
| 107 | + TextButton( |
| 108 | + onPressed: () => Navigator.of(context).pop(), |
| 109 | + child: Text('Cancel'), |
| 110 | + ), |
| 111 | + TextButton( |
| 112 | + onPressed: () async { |
| 113 | + await _dbHelper.deleteNote(id); |
| 114 | + _refreshNotes(); |
| 115 | + Navigator.of(context).pop(); |
| 116 | + }, |
| 117 | + child: Text('Delete'), |
| 118 | + ), |
| 119 | + ], |
| 120 | + ); |
| 121 | + }, |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + @override |
| 126 | + Widget build(BuildContext context) { |
| 127 | + return Scaffold( |
| 128 | + appBar: AppBar( |
| 129 | + title: Text('CRUD App'), |
| 130 | + ), |
| 131 | + body: _isLoading |
| 132 | + ? Center(child: CircularProgressIndicator()) |
| 133 | + : ListView.builder( |
| 134 | + itemCount: _notes.length, |
| 135 | + itemBuilder: (context, index) { |
| 136 | + final note = _notes[index]; |
| 137 | + return ListTile( |
| 138 | + title: Text(note.title), |
| 139 | + subtitle: Text(note.description), |
| 140 | + onTap: () => _addOrEditNoteDialog(note), |
| 141 | + trailing: IconButton( |
| 142 | + icon: Icon(Icons.delete), |
| 143 | + onPressed: () => _deleteNoteDialog(note.id!), |
| 144 | + ), |
| 145 | + ); |
| 146 | + }, |
| 147 | + ), |
| 148 | + floatingActionButton: FloatingActionButton( |
| 149 | + onPressed: () => _addOrEditNoteDialog(), |
| 150 | + child: Icon(Icons.add), |
| 151 | + ), |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
| 155 | + |
0 commit comments