1
answer
1
watching
134
views

Flutter Program ver 3.7.6 and Android Studio Eel ver 2022.1.1 Patch 1

Problem w/Flatbutton see screenshot below how to create class for that one and where do I put it. Below screenshot is the full flutter program.

Ā 

import 'package:flutter/material.dart';

void main() {
runApp(const Todo());
}

class Todo extends StatelessWidget {
const Todo({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(home: TodoList());
}
}

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

@override
_TodoListState createState() => _TodoListState();
}

class _TodoListState extends State<TodoList> {
// save data
final List<String> _todoList = <String>[];
// text field
final TextEditingController _textFieldController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('To-Do List')),
body: ListView(children: _getItems()),
// add items to the to-do list
floatingActionButton: FloatingActionButton(
onPressed: () => _displayDialog(context),
tooltip: 'Add Item',
child: const Icon(Icons.add)),
);
}

void _addTodoItem(String title) {
// Wrapping it inside a set state will notify
// the app that the state has changed
setState(() {
_todoList.add(title);
});
_textFieldController.clear();
}

// this Generate list of item widgets
Widget _buildTodoItem(String title) {
return ListTile(title: Text(title));
}

// display a dialog for the user to enter items
Future<Future> _displayDialog(BuildContext context) async {
// alter the app state to show a dialog
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Add a task to your list'),
content: TextField(
controller: _textFieldController,
decoration: const InputDecoration(hintText: 'Enter task here'),
),
actions: <Widget>[
// add button
FlatButton(
child: const Text('ADD'),
onPressed: () {
Navigator.of(context).pop();
_addTodoItem(_textFieldController.text);
},
),
// Cancel button
FlatButton(
child: const Text('CANCEL'),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
}
);
}
// iterates through our todo list title
List<Widget> _getItems() {
final List<Widget> todoWidgets = <Widget>[];
for (String title in _todoList) {
todoWidgets.add(_buildTodoItem(title));
}
return todoWidgets;
}
}

For unlimited access to Homework Help, a Homework+ subscription is required.

Avatar image
Liked by dragon2020 and 2 others

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in

Related questions

Related Documents

Weekly leaderboard

Start filling in the gaps now
Log in