我有一个ListView Builder,我想把TextEditingController的列表在一个列表



我有一个ListView Builder,我想在列表中放置TextEditingController的列表。但是它给出了一个错误,"期望找到一个标识符"。我困惑。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class ListViewBuilder extends StatefulWidget {
const ListViewBuilder({super.key});
@override
State<ListViewBuilder> createState() => _ListViewBuilderState();
}
class _ListViewBuilderState extends State<ListViewBuilder> {
final _formKey = GlobalKey<FormState>();
// final TextEditingController controller;
List validators = [
(value) {
if (value == null || value.isEmpty) {
return 'Please enter the number';
}
return null;
},
(value) {
if (value == null || value.isEmpty) {
return 'Please enter the email';
}
return null;
},
(value) {
if (value == null || value.isEmpty) {
return 'Please enter the password';
}
return null;
},
];
List formatters = [
FilteringTextInputFormatter.digitsOnly,
FilteringTextInputFormatter.allow(RegExp('[a-zA-Z]')),
FilteringTextInputFormatter.deny(RegExp(r'[/\]'))
];
///Error happens here!!
List controller = [
final _name = TextEditingController();
final _age = TextEditingController();
final _height = TextEditingController();
final _color = TextEditingController();
final _dateofBirt = TextEditingController();
final _Nationality = TextEditingController();
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Form(
key: _formKey,
child: ListView.builder(
itemCount: 3,
itemBuilder: (context, index) {
return TextFormField(
controller: controller[index],
validator: validators[index],
inputFormatters: [formatters[index]],
);
},
),
),
floatingActionButton: FloatingActionButton(
child: Text('Click'),
onPressed: () {
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
),
);
}
}

我将控制器设置为变量。并尝试单独分配它们,但仍然出现错误。

如果你喜欢使用list ofTextEditingController,你不需要把它赋给单独的变量。

List controller = [
TextEditingController(),
TextEditingController(),
TextEditingController(),
TextEditingController(),
TextEditingController(),
TextEditingController(),
];

您也可以使用List.generate

现在要获得您在第一项上定义的_name,您需要使用索引controller[0]作为_name。_ageforcontroller[1],,等等。

你可以得到像

这样的文本controller[index].text

相关内容

  • 没有找到相关文章

最新更新