List<>
Last updated
Last updated
// Some code
class TaskApp extends StatefulWidget {
const TaskApp({super.key});
@override
State<TaskApp> createState() => _TaskAppState();
}
class _TaskAppState extends State<TaskApp> {
TextEditingController taskController = TextEditingController();
TextEditingController editingController = TextEditingController();
final _formKey = GlobalKey<FormState>();
// CRUD - create - read - update - delete
// =,
// bool ? первый вариант : второй вариант
// методы -
List<String> tasks = ['Learn flutter', 'Hello', 'Say'];
void back() {
Navigator.pop(context);
}
void create() {
if (_formKey.currentState!.validate()) {
setState(() {
tasks.add(taskController.text);
taskController.clear();
back();
});
}
}
void delete({required int index}) {
setState(() {
tasks.removeAt(index);
back();
});
}
void update ({required int index}){
setState(() {
tasks[index] = editingController.text;
editingController.clear();
back();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: tasks.isEmpty
? Center(
child: Text(
'Пока задач нету ',
style: TextStyle(
fontSize: 30,
),
),
)
: ListView.builder(
itemCount: tasks.length,
itemBuilder: (context, index) {
return ListTile(
leading: Text(
'${index}:',
style: TextStyle(fontSize: 25),
),
title: Text(
tasks[index],
style: TextStyle(fontSize: 25),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Вы действительно хотите удалить?'),
content: Icon(
Icons.cancel_outlined,
color: Colors.red,
size: 40,
),
actions: [
TextButton(
onPressed: () {
back();
},
child: Text('Отмена'),
),
TextButton(
onPressed: () {
delete(index: index);
},
child: Text('Уадить'),
),
],
);
},
);
},
icon: Icon(
Icons.delete,
),
),
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
editingController.text = tasks[index];
return Form(
key: _formKey,
child: AlertDialog(
title: Text('Обновить'),
content: TextFormField(
controller: editingController,
validator: (value) {
if (value!.isEmpty) {
return 'Не может быть паустым';
}
return null;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'напишите задачу',
),
),
actions: [
TextButton(
onPressed: () {
back();
},
child: Text('Отмена'),
),
TextButton(
onPressed: () {
update(index: index);
},
child: Text('Обновить'),
),
],
),
);
},
);
},
icon: Icon(Icons.edit),
),
],
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Form(
key: _formKey,
child: AlertDialog(
title: Text('Создать задачу'),
content: TextFormField(
controller: taskController,
validator: (value) {
if (value!.isEmpty) {
return 'Не может быть паустым';
}
return null;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'напишите задачу',
),
),
actions: [
TextButton(
onPressed: () {
back();
},
child: Text('Отмена'),
),
TextButton(
onPressed: () {
create();
},
child: Text('Добавить'),
),
],
),
);
},
);
},
child: Text('Создать'),
),
);
}
}// Some code
class TaskApp extends StatefulWidget {
const TaskApp({super.key});
@override
State<TaskApp> createState() => _TaskAppState();
}
class _TaskAppState extends State<TaskApp> {
TextEditingController taskController = TextEditingController();
TextEditingController editingController = TextEditingController();
final _formKey = GlobalKey<FormState>();
// CRUD - create - read - update - delete
// =,
// bool ? первый вариант : второй вариант
// методы -
List<String> tasks = ['Learn flutter', 'Hello', 'Say'];
void back() {
Navigator.pop(context);
}
void create() {
if (_formKey.currentState!.validate()) {
setState(() {
tasks.add(taskController.text);
taskController.clear();
back();
});
}
}
void delete({required int index}) {
setState(() {
tasks.removeAt(index);
back();
});
}
void update({required int index}) {
setState(() {
tasks[index] = editingController.text;
editingController.clear();
back();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: tasks.isEmpty
? Center(
child: Text(
'Пока задач нету ',
style: TextStyle(
fontSize: 30,
),
),
)
: ListView.builder(
itemCount: tasks.length,
itemBuilder: (context, index) {
return ListTile(
leading: Text(
'${index}:',
style: TextStyle(fontSize: 25),
),
title: Text(
tasks[index],
style: TextStyle(fontSize: 25),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialogWidget(
title: 'удалить',
text: 'Удалить',
isShow: false,
onPressed: (){
delete(index: index);
},
);
},
);
},
icon: Icon(
Icons.delete,
),
),
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
editingController.text = tasks[index];
return Form(
key: _formKey,
child: AlertDialogWidget(
title: 'Обновить задачу',
controller: editingController,
hinText: 'Обновите задачу',
text: 'Обновить',
onPressed: () {
update(index: index);
},
),
);
},
);
},
icon: Icon(Icons.edit),
),
],
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Form(
key: _formKey,
child: AlertDialogWidget(
title: 'Создать Задачу',
controller: taskController,
hinText: 'напишите задачу',
text: 'Добавить',
onPressed: () {
create();
},
),
);
},
);
},
child: Text('Создать'),
),
);
}
}
class AlertDialogWidget extends StatelessWidget {
final String title;
final TextEditingController? controller;
final String? hinText;
final String text;
final Function() onPressed;
final bool isShow;
const AlertDialogWidget({
super.key,
required this.title,
this.controller,
this.hinText,
required this.text,
required this.onPressed,
this.isShow = true,
});
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Создать задачу'),
content: isShow
? TextFormField(
controller: controller,
validator: (value) {
if (value!.isEmpty) {
return 'Не может быть паустым';
}
return null;
},
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: hinText,
),
)
: Icon(
Icons.cancel_outlined,
color: Colors.red,
size: 45,
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Отмена'),
),
TextButton(
onPressed: onPressed,
child: Text(text),
),
],
);
}
}
class Buttons extends StatefulWidget {
const Buttons({super.key});
@override
State<Buttons> createState() => _ButtonsState();
}
class _ButtonsState extends State<Buttons> {
void nextPage(){
Navigator.push(context, MaterialPageRoute(builder: (context) => TaskApp()));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
ElevatedButton(onPressed: (){
nextPage();
}, child: Text(''))
],
),
);
}
}