BİLGİ YARIŞMASI- ALERT DIALOG
MAIN.DART
import 'package:bilgi/test_veri.dart';
import 'package:flutter/material.dart';
import 'constants.dart'; //CONSTANT İFADELER DOSYASI
//import 'soru.dart'; // soru class import edilmişti.
// Artık veriler test_1 ile TestVeri'den geliyor
void main() => runApp(BilgiTesti());
class BilgiTesti extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Bilgi Yarışması"),
centerTitle: true,
),
backgroundColor: Colors.blue[900],
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: SoruSayfasi(),
),
),
),
);
}
}
class SoruSayfasi extends StatefulWidget {
@override
_SoruSayfasiState createState() => _SoruSayfasiState();
}
class _SoruSayfasiState extends State<SoruSayfasi> {
List<Widget> secimler = []; // secimler listesi.
// soru class ile soru oluşturma şablonu yarattık
// TestVeri ile soru classını kullanarak soru bankası oluşturduk
// Soru bankasını çekebilmek için TestVeri tipini kullanıyoruz
TestVeri test_1 = TestVeri();
//int soruIndex = 0; // encapsulation işleminde test_veri dosyasına kopyaladık
void butonFonksiyonu(bool secilenButon) {
if (test_1.testBittiMi() == true) {
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
title: new Text("Bravo testi bitirdiniz"),
//content: new Text("Alert Dialog body"),
elevation: 10.0,
//yükseklik
backgroundColor: Colors.blue[100],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
contentPadding: EdgeInsets.only(top: 10.0),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Başa dön"),
onPressed: () {
Navigator.of(context).pop();
setState(() {
test_1
.testiSifirla(); // _soruIndex private olduğundan bu metodu çağırdık
secimler = []; // seçimler dizisindeki yüzleri boşalt
});
},
),
],
);
},
);
} else {
setState(() {
test_1.getSoruYaniti() == secilenButon //private _soruBankasi na ulaştık
//soruBankasi dizisi ile verilen cevabı karşılaştırma
? secimler.add(kDogruIcon)
: secimler.add(kYanlisIcon);
//secimler.add(kYanlisIcon);
test_1
.sonrakiSoru(); // soru sayısını artırma işi class içindeki metod ile yapılıyor
});
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 4,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
//test_1.soruBankasi[soruIndex].soruMetni.toString(),
test_1.getSoruMetni(), //private veriye erişmek için
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
),
),
),
// Row( // İKONLAR İÇİN YENİ ROW
Wrap(
// İKONLAR AŞAĞI DEVAM ETSİN DİYE ROW YERİNE WRAP
spacing: 4,
// yatay ikonlar arası boşluk
runSpacing: 4,
// dikey ikonlar arası boşluk
direction: Axis.horizontal,
// yatay ve dikeyde ifade doldurur
alignment: WrapAlignment.center,
// ortalama
children: secimler, // children içine yüzleri listeden ekledik
),
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 6.0),
child: Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: RaisedButton(
onPressed: () {
butonFonksiyonu(
false); // butonFonksiyonu na false gönder
},
padding: EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.red[400],
child: Icon(
Icons.thumb_down,
size: 16,
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 12.0),
child: RaisedButton(
//DOĞRU CEVAP BUTONU
onPressed: () {
butonFonksiyonu(
true); // butonFonksiyonu na false gönder
},
padding: EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green[400],
child: Icon(
Icons.thumb_up,
size: 16,
),
),
),
)
],
),
),
)
],
);
}
}
TEST_VERI.DART// soru metni ve cevapları tuttuğumuz class
import 'package:bilgi/soru.dart'; // soru nesnesi için import ettik
class TestVeri {
int _soruIndex = 0; //hangi soruda olduğumuzu tutacak sayaç
// encapsulation için başına _ ekledik
//Soru class kullanarak soruBankasi oluşturduk
List<Soru> _soruBankasi = [
//değişkeni private yapmak için _ ekledik (ENCAPSULATION)
Soru(soruMetni: "Dünya yuvarlaktır.", soruYaniti: true),
Soru(soruMetni: "Kelebeklerin ömrü bir gündür.", soruYaniti: false),
Soru(soruMetni: "Titanic en büyük gemidir.", soruYaniti: false),
Soru(soruMetni: "Ankara başkenttir.", soruYaniti: true),
Soru(soruMetni: "Eylül sonbahardır.", soruYaniti: true),
Soru(soruMetni: "Gemiler uçabilir.", soruYaniti: true)
];
// private değişken olan _soruBankasi soruMetni ne erişim için metod yazdık
String getSoruMetni(/* int soruIndex */) {
// soruIndex i yukarıda tanımladık başka yerden
// çağrılmayacağı için parametre olarak kalmasına gerek yok
return _soruBankasi[_soruIndex].soruMetni.toString();
}
bool? getSoruYaniti(/*int soruIndex*/) {
return _soruBankasi[_soruIndex].soruYaniti;
}
// kaçıncı soru olduğunu metod ile kontrol ediyoruz
void sonrakiSoru() {
if (_soruIndex + 1 < _soruBankasi.length) {
_soruIndex++;
}
}
bool testBittiMi() {
if (_soruIndex + 1 >= _soruBankasi.length) {
return true;
} else {
return false;
}
}
void testiSifirla(){
_soruIndex=0;
}
}SORU.DART// soru ve cevaplar için şablon hazırladığımız class
// class ayrı dosya yapıldığında material.dart import edilir
import 'package:flutter/material.dart';
class Soru {
String? soruMetni; //instance variables
bool? soruYaniti;
Soru(
{@required this.soruMetni,
@required this.soruYaniti}); //zorunlu named parametreli constructor
}
CONSTANTS.DART// doğru ve yanlış iconları tanımladığımız class
// CONSTANT.DART DOSYASI
import 'package:flutter/material.dart'; // iconlar material kütüphanesinin içinde
const Icon kDogruIcon = Icon(Icons.mood, color: Colors.green); // değişmeyecek (const) ikonları global değişkenlere atadık
const Icon kYanlisIcon = Icon(Icons.mood_bad, color: Colors.red); // tüm klasların altına
// YAZIM GELENEĞİ OLARAK CONSTANT İFADE ADLARININ BAŞINA k EKLENİR