👨‍🏫10 List - список task app part 2

import 'package:flutter/material.dart';

class Lesson22 extends StatefulWidget {
  const Lesson22({super.key});

  @override
  State<Lesson22> createState() => _Lesson22State();
}

class _Lesson22State extends State<Lesson22> {
  TextEditingController taskController = TextEditingController();
  TextEditingController updateController = TextEditingController();

  List<String> tasks = [
    'Нуржигит', // 0
    'Learn english',
    'Изучить flutter',
    'Learn english',
    'Изучить flutter',
    'Learn english',
    'Изучить flutter',
    'Learn english',
  ];

  // =, +=, -=, *=, /=
  // ==, >

  // tasks[5] = 'Madina';
  // tasks[0] += 2;

  String name = 'Hello';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          Row(
            children: [
              TextButton(
                onPressed: () {
                  setState(() {
                    tasks.clear();
                  });
                },
                child: Text('Delete all'),
              ),
            ],
          ),
          Expanded(
            child: tasks.isEmpty
                ? Center(child: Text('Нету задач'))
                : ListView.builder(
                    shrinkWrap: true,
                    itemCount: tasks.length, //8
                    itemBuilder: (context, index) {
                      return ListTile(
                        leading: Checkbox(value: false, onChanged: (value) {}),
                        title: Text(tasks[index]),
                        trailing: Row(
                          mainAxisSize: .min,
                          children: [
                            IconButton(
                              onPressed: () {
                                showDialog(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                      title: Text(
                                        'Вы действительно хотите удлаить?',
                                      ),
                                      content: Icon(
                                        Icons.crisis_alert,
                                        color: Colors.red,
                                        size: 50,
                                      ),
                                      actions: [
                                        TextButton(
                                          onPressed: () {
                                            Navigator.pop(context);
                                          },
                                          child: Text('Нет'),
                                        ),
                                        TextButton(
                                          onPressed: () {
                                            setState(() {
                                              tasks.removeAt(index);
                                            });
                                            Navigator.pop(context);
                                          },
                                          child: Text('Да'),
                                        ),
                                      ],
                                    );
                                  },
                                );
                              },
                              icon: Icon(Icons.delete, color: Colors.red),
                            ),
                            IconButton(onPressed: (){
                              showDialog(context: context, builder: (context){
                                updateController.text = tasks[index];
                                return AlertDialog(
                                  title: Text('Обновить данные'),
                                  content: Column(
                                    mainAxisSize: .min,
                                    children: [
                                      Text('Обновите задачу'),
                                      TextFormField(
                                        controller: updateController,
                                      ),
                                    ],
                                  ),
                                  actions: [
                                    TextButton(onPressed: (){
                                      Navigator.pop(context);
                                    }, child: Text('Нет')),
                                    TextButton(onPressed: (){
                                      setState(() {
                                        tasks[index] = updateController.text;
                                      });
                                      Navigator.pop(context);
                                      updateController.clear();
                                    }, child: Text('Да'))
                                  ],
                                );
                              });
                            }, icon: Icon(Icons.edit))
                          ],
                        ),
                      );
                    },
                  ),
          ),
        ],
      ),
      floatingActionButton: ElevatedButton(
        onPressed: () {
          showModalBottomSheet(
            context: context,
            builder: (context) {
              return Container(
                height: 400,
                width: double.infinity,
                padding: EdgeInsets.all(20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(20),
                    topLeft: Radius.circular(20),
                  ),
                  color: Colors.white,
                ),
                child: Column(
                  children: [
                    TextFormField(
                      controller: taskController,
                    ),
                    TextButton(onPressed: (){
                      setState(() {
                        tasks.add(taskController.text);
                      });
                      Navigator.pop(context);
                    }, child: Text('Create'))
                  ],
                ),
              );
            },
          );
        },
        child: Text('Create task'),
      ),
    );
  }
}

Last updated