Fluttera yeni başladım ve bir öğrenci not takip sistemi yapıyoruz eğitimlerle ama bir sorunum var öğrenci ekle veya öğrenci güncelleye tıkladığımda güncelle/veya ekleme sayfasına gidiyor eklemeyi yapıyor ama setState çalışmıyor setStatenin çalışması için listeye bir kere tıklamam gerekiyor ondan sonra yaptığım güncellemeler geliyor ama ben güncelleme/ekleme işlemi bittiği anda setstatenin listemi yeniden çizmesini istiyorum bu konuda bana yardımcı olabilecek biri var mı ?
anasayfa kodu ;
import ‘dart:async’;
import ‘package:flutter/material.dart’;
import ‘package:flutter_application_2/screens/studentAdd.dart’;
import ‘package:flutter_application_2/screens/studentChange.dart’;
import ‘package:flutter_application_2/students.dart’;
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String seciliOgrenci = "";
Students selectedStudent= Students.withId(0, “0”, “0”, 0, “https://images.unsplash.com/photo-1519058082700-08a0b56da9b4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NDR8fHBvcnRyYWl0fGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60”);
List<Students> student = [
Students.withId(1,“Gorkem”, “Arslanbogan”, 85,
“https://images.unsplash.com/photo-1519058082700-08a0b56da9b4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NDR8fHBvcnRyYWl0fGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60”),
Students.withId(2,“Aysude”, “Arslan”, 100,
“https://images.unsplash.com/photo-1526510747491-58f928ec870f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Njh8fHBvcnRyYWl0fGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60”),
Students.withId(3,“Engin”, “Payas”, 40,
“https://images.unsplash.com/photo-1531427186611-ecfd6d936c79?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8N3x8cG9ydHJhaXR8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60”),
Students.withId(4,“İlyas”, “Alabıyık”, 20,
“https://images.unsplash.com/photo-1500648767791-00dcc994a43e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTZ8fHBvcnRyYWl0fGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60”)
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“Öğrenci Takip Sistemi”),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: student.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(student[index].firstName + student[index].lastName),
// elemanlarımızı liste şeklinde düzenli gösterdik.
leading: CircleAvatar(
backgroundImage: NetworkImage(""),
// profil resmini fonksiyonel olarak değişken üzerinden çektik.
),
//leading özelliği listenin başına widget eklemek için kullanılır.
// CircleAvatar widget resim yada renk eklemek istersek kullanırız.
subtitle: Text("Sınav Notu: " +
student[index].puanDurumu.toString() +
" (" +
student[index].getdurum +
")"),
// title metinin altındaki yere yazı yazdırabileceğimiz özellik.
trailing: gectiKaldi(student[index].puanDurumu),
// listemizdeki elemanların sağ tarafına başka bir widget atamak için kullandık.
onTap: (){
setState(() {
seciliOgrenci = student[index].firstName +" "+ student[index].lastName;
});
selectedStudent = student[index];
print(seciliOgrenci);
},
);
}),
),
Text(“Seçili Öğrenci: ” + selectedStudent.firstName +“ ” + selectedStudent.lastName),
Row(
children: [
Flexible(
fit: FlexFit.tight,
flex: 2,
child: RaisedButton(
color: Colors.green,
child: Row(
children: [
Icon(Icons.add),
SizedBox(
width: 5,
),
Text(“Ekle”),
],
),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => StudentAdd(student)));
// bu kod bizim öğrenci ekle sayfamıza götürücek uygulama içinde
}
)
),
Flexible(
fit: FlexFit.tight,
flex: 2,
child: RaisedButton(
color: Colors.black12,
child: Row(
children: [
Icon(Icons.update),
SizedBox(
width: 5,
),
Text(“Güncelle”),
],
),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => StudentChange(selectedStudent)));
})),
Flexible(
fit: FlexFit.tight,
flex: 2,
child: RaisedButton(
color: Colors.amberAccent,
child: Row(
children: [
Icon(Icons.delete),
SizedBox(
width: 5,
),
Text(“Sil”),
],
),
onPressed: () {
setState(() {
student.remove(selectedStudent);
});
print("Yazdı.");
}))
],
)
Öğrenci ekleme Dosyası ;
import ‘dart:async’;
import ‘package:flutter/material.dart’;
import ‘package:flutter_application_2/validation/student_validation.dart’;
import ‘package:flutter_application_2/students.dart’;
class StudentAdd extends StatefulWidget {
late List<Students> students;
StudentAdd(List<Students> students) {
this.students = students;
}
@override
State<StatefulWidget> createState() {
// ignore: no_logic_in_create_state
return _StudentAddState(students);
}
}
// normalde bir class yalnızca başka bir classtan inherith edilebilir özelliklerini alabilir.
// ama with class_ismi dersen 3. bir classın fonksiyonlarını kullanabilirsin.
class _StudentAddState extends State with StudentValidationMixin {
var student = Students.withoutInfo();
// boş bir student nesnesi oluşturduk yeni öğrenciyi bu nesneye atıcaz.
var formKey = GlobalKey<FormState>();
// formun içine dışardaki fonksiyonların ulaşması için anahtar bıraktık.
late List<Students> students;
_StudentAddState(List<Students> students) {
this.students = students;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“Yeni Öğrenci Ekle”),
),
body: Center(
child: Container(
// alt alta olmasını istediğimiz için container kullandık.
margin: EdgeInsets.all(20), // formlar arası boşlukları verdik
child: Form(
key:
formKey, // formun içine dışardaki fonksiyonların ulaşması için anahtar bıraktık.
// form oluşturduk
child: Column(children: <Widget>[
TextFormField(
// formun input kısmını oluşturmak için kullandık ve inputdecoration kullandık.
decoration: InputDecoration(
labelText: "Öğrenci adı: ", hintText: "Görkem"),
validator: validationFirstName,
onSaved: (String? value) {
if (value == null || value.isEmpty) {
return;
} else {
student.firstName = value;
}
}),
TextFormField(
decoration: InputDecoration(
labelText: "Öğrensi soyadı:",
hintText: "Arslanboğan",
),
validator: validationLastName,
// formda yazılacak bilginin kurallarını classla validatöre atadık.
onSaved: (String? value) {
if (value == null || value.isEmpty) {
return;
} else {
student.lastName = value;
}
}
//onSaved: fonksiyonu bilgileri aldıktan sonra napacağımızı söylemek için kullanırız.
),
TextFormField(
decoration: InputDecoration(
labelText: "Öğrenci Notu:", hintText: "Örn : 75"),
validator: validationGrade,
onSaved: (String? value) {
if (value == null || value.isEmpty) {
return;
} else {
student.puanDurumu = int.parse(value);
}
},
),
saveButton(),
]),
),
),
),
);
}
Widget saveButton() {
return RaisedButton(
child: Text(“Kaydet”),
onPressed: () {
if (formKey.currentState!.validate()) {
//formKey.currentState!.validate() bu kod üstteki formlardaki validatörleri çalıştırıp kurallar doğrumu kontrol ediyor.
// eğer kurallar doğruysa true döndürüyor değilse false döndürüyor.
formKey.currentState!.save();
//formKey.currentState!.save(); bu kodda formlardaki onSaved: kısmında yapılması gereken işleri yapıyor.
students.add(student);
Navigator.pop(context);
}
},
);
}
}