因此,我试图有一个页面,你点击浮动操作按钮,弹出对话框/警报对话框出现一个文本字段,你输入名称,它在列表视图中创建一个项目,我也希望该项目是可点击的,并前往另一个页面,该名称作为新的应用程序栏名称。
这是我的代码,但它没有创建任何东西:
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
class lists extends StatefulWidget {
const lists({super.key});
@override
State<lists> createState() => _listsState();
}
class _listsState extends State<lists> {
@override
Widget build(BuildContext context) {
String _newItemName = '';
final List<String> _items = [];
final textController = TextEditingController();
int _currentIndex = 0;
return Scaffold(
body: _items.length > 0
? ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${_items[index]}'),
);
},
)
: const Center(
child: Text("You currently have no classes. Add from below."),
),
floatingActionButton: SpeedDial(
animatedIcon: AnimatedIcons.menu_arrow,
spacing: 6,
spaceBetweenChildren: 6,
backgroundColor: const Color.fromARGB(255, 22, 37, 50),
foregroundColor: const Color.fromARGB(255, 255, 255, 255),
children: [
SpeedDialChild(
child: const Icon(Icons.group_add), label: "add student"),
SpeedDialChild(
child: const Icon(Icons.school),
label: "add class",
onTap: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Add a new class'),
content: TextField(
controller: textController,
autofocus: true,
decoration: const InputDecoration(
hintText: "Enter the name of the class."),
),
actions: [
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
TextButton(
child: Text('Add'),
onPressed: () {
Navigator.pop(context, textController.text);
},
),
],
);
},
);
},
)
],
),
);
}
}
2条答案
按热度按时间cmssoen21#
在
Add
操作中,使用setState
更新_list
的值以包含该项flvlnr442#