Files
todo/lib/main.dart

97 lines
2.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:todo/utils/dialog_box.dart';
import 'package:todo/utils/todo_tile.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Todo',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = TextEditingController();
List todoList = [
["Make Tutorial", true],
["Buy car", false],
["Buy new wife", true],
["buy card", false],
];
void checkBoxChanged(bool? value, int index) {
setState(() {
todoList[index][1] = !todoList[index][1];
});
}
void saveNewTask() {
setState(() {
todoList.add([_controller.text, false]);
_controller.clear();
});
Navigator.of(context).pop();
}
void createNewTask() {
showDialog(
context: context,
builder: (context) {
return DialogBox(
controller: _controller,
onCancel: () => Navigator.of(context).pop(),
onSave: saveNewTask,
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: todoList.length,
itemBuilder: (context, index) {
return ToDoTile(
taskName: todoList[index][0],
taskCompleted: todoList[index][1],
onChanged: (value) => checkBoxChanged(value, index),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: createNewTask,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}