Menu
CoddyTech

Dart'ta Ve, Veya ve Değil

Coddy'nin Flutter Journey'sinin Etkileşimli Uygulamalar bölümünün bir parçası. Ders 19 / 34.

Çoğu zaman tek bir bool yeterli olmaz. Dart, bu değerleri üç operatörle birleştirir:

  • a && b (ve): yalnızca ikisi de true olduğunda true olur
  • a || b (veya): en az biri true olduğunda true olur
  • !a (değil): true ile false değerlerini tersine çevirir
bool _hasTicket = true;
bool _isLate = false;

_hasTicket && !_isLate   // true: biniş yapabilirsiniz
_hasTicket || _isLate    // true: en az biri doğrudur

Sonuç bir bool değeridir; bu nedenle koşullu ifadenin ? işaretinden önce veya bir if içinde kullanılabilir. Parantezler okumayı kolaylaştırır:

Text((_hasTicket && !_isLate) ? 'Board now' : 'Wait')
challenge icon

Görev

Kolay

Kapı yalnızca anahtar ve kodla açılır. Şu iki satırı değiştir:

  • İlk satır, anahtarın ve kodu bildiğinde The door is open, aksi hâlde The door is locked der
  • İkinci satır, anahtarın veya kodu bildiğinde Getting closer, aksi hâlde Find the key and the code der

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,
      home: const HomePage(),
    );
  }
}

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _hasKey = false;
  bool _knowsCode = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Door')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text('The door is locked', style: TextStyle(fontSize: 24)),
            const Text('Find the key and the code'),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _hasKey = true;
                });
              },
              child: const Text('Take the key'),
            ),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _knowsCode = true;
                });
              },
              child: const Text('Learn the code'),
            ),
          ],
        ),
      ),
    );
  }
}
quiz iconKendini test et

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

Etkileşimli Uygulamalar bölümündeki tüm dersler

Kendi başına pratik yap: Online Flutter derleyicisi