Menu
CoddyTech

Bilgi Yarışması: Doğru mu Yanlış mı

Coddy'nin Flutter Journey'sinin Projeler bölümünün bir parçası. Ders 11 / 36.

Bir yanıt seç; test hemen sonucu göstermeli: doğru yanıt verdiysen yeşil, yanlış yanıt verdiysen kırmızı, her iki durumda da doğru yanıt yeşil olur; böylece öğrenirsin.

Durumun, hangi yanıtı seçtiğini hatırlaması gerekir. int? _picked, bir yanıt seçene kadar null değerindedir; bu nedenle _picked != null, yanıt verdiğin anlamına gelir. Yanıt verdikten sonra düğmeler artık seçimlerini dinlemez: _pick işlevinin ilk satırı bir koruma koşuludur.

void _pick(int answer) {
  if (_answered) return;
  setState(() {
    _picked = answer;
  });
}

Küçük bir işlev, her düğmenin rengini belirler. null, "kendime ait bir renk yok" anlamına gelir ve düğme tema rengini korur:

Color? _colorFor(int answer) {
  if (!_answered) return null;
  if (answer == _question.correct) return Colors.green;
  if (answer == _picked) return Colors.red;
  return null;
}

Düğmelerin artık yalnızca metne değil, konumlarına da ihtiyacı var; bu yüzden döngü sayar: for (int i = 0; i < question.answers.length; i++), i değeri 0, 1, 2 ve 3 olacak şekilde çalışır.

challenge icon

Görev

Zor

Yanıtları renklendir:

  • kuramdan int? _picked öğesini, _question ve _answered getter’larını ve _pick ile _colorFor yöntemlerini ekle
  • yanıtlar üzerinde konumlarına göre for (int i = 0; ...) ile döngü kur
  • her düğme _pick(i) çağrısı yapar; backgroundColor değeri _colorFor(i) olur ve düğmenin bir rengi varsa metni beyaz olur

Kendin dene

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const QuizScreen(),
    );
  }
}

class Question {
  const Question(this.text, this.answers, this.correct);

  final String text;
  final List<String> answers;
  final int correct;
}

const List<Question> questions = [
  Question('Which widget stacks its children from top to bottom?', ['Row', 'Column', 'Stack', 'Wrap'], 1),
  Question('Which language do you write Flutter apps in?', ['Kotlin', 'Swift', 'Dart', 'JavaScript'], 2),
  Question('What does setState do?', ['Rebuilds the widget', 'Saves the app', 'Closes the screen', 'Plays a sound'], 0),
  Question('Which widget shows a list you can scroll?', ['Container', 'SizedBox', 'Padding', 'ListView'], 3),
  Question('Which widget adds space around its child?', ['Padding', 'Center', 'Icon', 'Text'], 0),
];

class QuizScreen extends StatefulWidget {
  const QuizScreen({super.key});

  @override
  State<QuizScreen> createState() => _QuizScreenState();
}

class _QuizScreenState extends State<QuizScreen> {
  int _index = 0;

  @override
  Widget build(BuildContext context) {
    final question = questions[_index];
    final textTheme = Theme.of(context).textTheme;
    return Scaffold(
      appBar: AppBar(title: const Text('Flutter Quiz')),
      body: Padding(
        padding: const EdgeInsets.all(24),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(question.text, style: textTheme.headlineSmall),
            const SizedBox(height: 24),
            for (String answer in question.answers)
              Padding(
                padding: const EdgeInsets.only(bottom: 12),
                child: FilledButton.tonal(
                  style: FilledButton.styleFrom(
                    padding: const EdgeInsets.all(16),
                  ),
                  onPressed: () {},
                  child: Text(answer),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Projeler bölümündeki tüm dersler

Kendi başına pratik yap: Online Flutter derleyicisi