Untitled

 avatar
unknown
plain_text
a year ago
5.1 kB
4
Indexable
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class ToDo {
  String title;
  String subtitle;

  ToDo({required this.title, required this.subtitle});
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: ToDoListPage(),
    );
  }
}

class ToDoListPage extends StatefulWidget {
  @override
  _ToDoListPageState createState() => _ToDoListPageState();
}

class _ToDoListPageState extends State<ToDoListPage> {
  List<ToDo> toDoList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Todo List'),
      ),
      body: ListView.builder(
        itemCount: toDoList.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(toDoList[index].title),
            subtitle: Text(toDoList[index].subtitle),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                IconButton(
                  icon: Icon(Icons.edit),
                  onPressed: () {
                    _editToDoItem(index);
                  },
                ),
                IconButton(
                  icon: Icon(Icons.delete),
                  onPressed: () {
                    _deleteToDoItem(index);
                  },
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _showAddToDoDialog();
        },
        child: Icon(Icons.add),
      ),
    );
  }

  Future<void> _showAddToDoDialog() async {
    String title = '';
    String subtitle = '';

    await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Add ToDo'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                decoration: InputDecoration(labelText: 'Title'),
                onChanged: (value) => title = value,
              ),
              TextField(
                decoration: InputDecoration(labelText: 'Subtitle'),
                onChanged: (value) => subtitle = value,
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Cancel'),
            ),
            TextButton(
              onPressed: () {
                if (title.isNotEmpty && subtitle.isNotEmpty) {
                  setState(() {
                    toDoList.add(ToDo(title: title, subtitle: subtitle));
                  });
                  Navigator.pop(context);
                } else {
                  // Show an error message or handle the case where
                  // either title or subtitle is null or empty.
                  // For now, just print an error message.
                  print('Title and subtitle cannot be empty.');
                }
              },
              child: Text('Save'),
            ),
          ],
        );
      },
    );
  }

  Future<void> _editToDoItem(int index) async {
    String title = toDoList[index].title;
    String subtitle = toDoList[index].subtitle;

    await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text('Edit ToDo'),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextField(
                decoration: InputDecoration(labelText: 'Title'),
                controller: TextEditingController(text: title),
                onChanged: (value) => title = value,
              ),
              TextField(
                decoration: InputDecoration(labelText: 'Subtitle'),
                controller: TextEditingController(text: subtitle),
                onChanged: (value) => subtitle = value,
              ),
            ],
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Cancel'),
            ),
            TextButton(
              onPressed: () {
                if (title.isNotEmpty && subtitle.isNotEmpty) {
                  setState(() {
                    toDoList[index] = ToDo(title: title, subtitle: subtitle);
                  });
                  Navigator.pop(context);
                } else {
                  // Show an error message or handle the case where
                  // either title or subtitle is null or empty.
                  // For now, just print an error message.
                  print('Title and subtitle cannot be empty.');
                }
              },
              child: Text('Save'),
            ),
          ],
        );
      },
    );
  }

  void _deleteToDoItem(int index) {
    setState(() {
      toDoList.removeAt(index);
    });
  }
}
Editor is loading...
Leave a Comment