플러터 Flutter ToDo List 구현하기

반응형
  • 코드 작성
import 'dart:async';
import 'package:flutter/material.dart';

class ToDoListPage extends StatefulWidget {
  const ToDoListPage({Key? key}) : super(key: key);

  @override
  State<ToDoListPage> createState() => _ToDoListPageState();
}

class _ToDoListPageState extends State<ToDoListPage> {
  final _todoController = TextEditingController();
  List<String> _todoList = [];

  void _addTodo() {
    setState(() {
      _todoList.add(_todoController.text);
      _todoController.text = '';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('To-Do List'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(10),
            child: TextField(
              controller: _todoController,
              decoration: InputDecoration(
                hintText: 'Enter a to-do',
              ),
            ),
          ),
          ElevatedButton(
            child: Text('Add'),
            onPressed: _addTodo,
          ),
          Expanded(
            child: ListView.builder(
              itemCount: _todoList.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(_todoList[index]),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
반응형