Here’s an example of a basic to-do list app in Flutter:

import ‘package:flutter/material.dart’;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Todo List’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: TodoList(),
);
}
}

class TodoList extends StatefulWidget {
@override
_TodoListState createState() => _TodoListState();
}

class _TodoListState extends State<TodoList> {
List<String> _todos = [];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Todo List’),
),
body: ListView.builder(
itemCount: _todos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_todos[index]),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
_todos.removeAt(index);
});
},
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(‘Add todo’),
content: TextField(
onSubmitted: (value) {
setState(() {
_todos.add(value);
});
Navigator.of(context).pop();
},
),
actions: <Widget>[
FlatButton(
child: Text(‘Cancel’),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
child: Icon(Icons.add),
),
);
}
}

 

 

In this example, a TodoList widget is created which extends StatefulWidget. The state of the widget is managed by _TodoListState. The _todos list stores the to-do items. The UI consists of a list of the to-do items and a floating action button to add new items to the list. When the floating action button is pressed, a dialog pops up to allow the user to enter the name of a new to-do item, which is then added to the _todos list. The list of to-do items is displayed using a ListView.builder. Each item in the list has a delete icon, which removes the item from the _todos list when pressed.

Facebook Comments Box
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments