我试图在列表中获取学生的值,并在下拉列表中加载他的教室。 当我的学生不在默认教室 (SIO1) 时,教室显示是错误的。 我点击笔,编辑当前学生以及定义他的所有参数的值。
请参阅以下代码...感谢您的宝贵时间!
import 'package:flutter/material.dart';
import 'models/Database.dart';
import 'controllers/PersonController.dart';
import 'models/Person.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
final TextEditingController _nameController = TextEditingController();
final TextEditingController _ageController = TextEditingController();
final PersonController _personController = PersonController();
final DatabaseHelper _databaseHelper = DatabaseHelper();
final TextEditingController _classController = TextEditingController();
bool _isUpdating = false;
int _selectedPersonId = 0;
String _selectedClass = 'SIO1'; // Classe par défaut
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
margin: const EdgeInsets.all(10), // Marge autour du formulaire
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
// Bordure autour du formulaire
borderRadius: BorderRadius.circular(
10), // Coins arrondis du formulaire
),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: _nameController,
decoration: const InputDecoration(labelText: 'Nom'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Entrer votre nom';
}
return null;
},
),
TextFormField(
controller: _ageController,
decoration: const InputDecoration(labelText: 'Age'),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Entrer votre age';
}
return null;
},
),
const SizedBox(height: 20),
const Text(
'Votre classe',
style: TextStyle(
fontSize: 16,
),
),
DropdownButtonFormField<String>(
value: _selectedClass,
onChanged: (String? newValue) {
setState(() {
_selectedClass = newValue!;
});
},
items: <String>['SIO1', 'SIO2']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: _isUpdating ? _updatePerson : _addPerson,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
textStyle: const TextStyle(fontSize: 18,),
padding: const EdgeInsets.symmetric(
horizontal: 30, vertical: 15),
),
child: Text(
_isUpdating ? 'Mise à jour de l\'etudiant' : 'Ajout d\'un nouveau étudiant',
style: const TextStyle(
fontSize: 18, color: Colors.white),
),
),
),
),
],
),
),
),
Expanded(
child: FutureBuilder<List<Person>>(
future: _databaseHelper.getPersons(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
final person = snapshot.data![index];
return ListTile(
title: Text(person.name),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Age: ${person.age}'),
Text('Classe: ${person.studentClass}'),
// Ajout de la classe d'étudiants
],
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
setState(() {
_isUpdating = true;
_selectedPersonId = person.id;
_nameController.text = person.name;
_ageController.text = person.age.toString();
});
},
),
IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
await _personController.deletePerson(
context, person.id);
// Rafraîchir la liste après suppression
setState(() {});
},
),
],
),
);
},
);
}
},
),
),
],
),
),
);
}
void _addPerson() async {
String name = _nameController.text;
int age = int.parse(_ageController.text);
String selectedClass = _selectedClass;
await _personController.addPerson(context, name, age, selectedClass);
_clearForm();
}
void _updatePerson() async {
String name = _nameController.text;
int age = int.parse(_ageController.text);
String selectedClass = _selectedClass;
await _personController.updatePerson(
context, _selectedPersonId, name, age, selectedClass);
_clearForm();
}
void _clearForm() {
_nameController.clear();
_ageController.clear();
setState(() {
_isUpdating = false;
_selectedPersonId = 0;
_selectedClass = 'SIO1';
});
}
}
如果我正确理解问题,您希望在单击编辑按钮时在
person's classroom
上显示 dropdown
。
问题是您没有在
_selectedClass
上设置setState
的值。像这样更改编辑按钮:
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
setState(() {
_isUpdating = true;
_selectedPersonId = person.id;
_nameController.text = person.name;
_ageController.text = person.age.toString();
_selectedClass = person.studentClass; // Added this one
});
},
),