import 'package:flutter/material.dart'; void main() { runApp(NoteApp()); } class NoteApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Note App', theme: ThemeData( primarySwatch: Colors.blue, ), home: NoteScreen(), ); } } class NoteScreen extends StatefulWidget { @override _NoteScreenState createState() => _NoteScreenState(); } class _NoteScreenState extends State<NoteScreen> { List<String> notes = []; void addNote() { showDialog( context: context, builder: (BuildContext context) { String newNote; return AlertDialog( title: Text('Add Note'), content: TextField( onChanged: (value) { newNote = value; }, ), actions: [ TextButton( onPressed: () { setState(() { notes.add(newNote); }); Navigator.of(context).pop(); }, child: Text('Add'), ), ], ); }, ); } void deleteNoteAt(int index) { setState(() { notes.removeAt(index); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Notes'), ), body: ListView.builder( itemCount: notes.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text(notes[index]), trailing: IconButton( icon: Icon(Icons.delete), onPressed: () => deleteNoteAt(index), ), ); }, ), floatingActionButton: FloatingActionButton( onPressed: addNote, child: Icon(Icons.add), ), ); } }
In this example, we have a Flutter application called "Note App" that allows users to take and manage notes. The main functionality includes adding notes, deleting notes, and displaying a list of notes.
The NoteApp
class is the root widget for the application, and it sets up the basic theme and the home screen, which is NoteScreen
.
The NoteScreen
class is a stateful widget that maintains a list of notes. The addNote
function displays a dialog with a text field where the user can enter a new note. When the user taps the "Add" button, the note is added to the list, and the dialog is closed.
The deleteNoteAt
function removes a note from the list when the user taps the delete icon associated with that note.
The build
method in _NoteScreenState
builds the UI for the NoteScreen
. It displays a ListView of notes, where each note is shown as a ListTile with a delete icon. Tapping the delete icon triggers the corresponding deleteNoteAt
function. The floatingActionButton
is used to add new notes by calling the addNote
function.
This is a basic implementation, and you can enhance it further by adding features like editing notes, persisting notes using a database or file system, adding categories or tags to notes, etc.
GitHub