What this code has done:
- Can create dismissible lists
In the main.dart
file
import 'package:flutter/material.dart';
import 'package:task_logger/screens/task_list_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'This is for Android',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.blue,
accentColor: Colors.orange),
home: TaskListScreen(),
);
}
}
In the task_list_screen.dart
import 'package:flutter/material.dart';
class TaskListScreen extends StatefulWidget {
@override
_TaskListScreenState createState() => _TaskListScreenState();
}
class _TaskListScreenState extends State<TaskListScreen> {
List tasks = List();
String input = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("What are you going to do now?"),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
title: Text("Add Task"),
content: TextField(
autofocus: true,
onChanged: (String value) {
input = value;
},
),
actions: <Widget>[
FlatButton(
child: Text("Add"),
onPressed: () {
setState(() {
tasks.add(input);
});
Navigator.of(context)
.pop(); // Remove the dialog after tapping the Add button
},
),
FlatButton(
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
},
child: Icon(
Icons.add,
color: Colors.white,
),
),
body: ListView.builder(
itemCount: tasks.length,
itemBuilder: (BuildContext context, int index) {
return Dismissible(
// Allow you to remove a task by swiping
onDismissed: (DismissDirection direction) {
setState(() {
tasks.removeAt(index);
});
},
background: Container(),
secondaryBackground: Container(
child: Icon(
Icons.delete,
color: Colors.white,
),
color: Colors.red,
),
key: Key(tasks[index]),
child: Card(
elevation: 2.0,
margin: EdgeInsets.all(2.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0)),
child: ListTile(
title: Text(tasks[index]),
trailing: IconButton(
icon: Icon(
Icons.delete,
color: Colors.red,
),
onPressed: () {
setState(() {
tasks.removeAt(index);
});
}),
),
));
}),
);
}
}
Comments