我有一张评分表。所以我必须在每个文本字段中保存不同的标记。尝试过控制器,但没有用。如果您有任何灵魂,请帮助。
为每个标记创建不同的控制器,最后处理它
import 'package:flutter/material.dart';
class MarkEntryForm extends StatefulWidget {
const MarkEntryForm({super.key});
@override
_MarkEntryFormState createState() => _MarkEntryFormState();
}
class _MarkEntryFormState extends State<MarkEntryForm> {
final TextEditingController _mathsController = TextEditingController();
final TextEditingController _scienceController = TextEditingController();
final TextEditingController _englishController = TextEditingController();
double _mathsMark = 0;
double _scienceMark = 0;
double _englishMark = 0;
void _saveMarks() {
_mathsMark = double.parse(_mathsController.text);
_scienceMark = double.parse(_scienceController.text);
_englishMark = double.parse(_englishController.text);
// Save the marks to the database or do other processing here
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Mark Entry Form'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _mathsController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Maths',
),
),
TextField(
controller: _scienceController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Science',
),
),
TextField(
controller: _englishController,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'English',
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _saveMarks,
child: const Text('Save Marks'),
),
],
),
),
);
}
}
这是一个存储一组标记的示例程序,您可以将其更改为 LIst 以存储更多数据,我们也可以使用 listview.builder 来显示它