Flutter GetX – Dialog

Flutter GetX paketi, kolay ve hızlı bir şekilde dialoglar oluşturmak için kullanabileceğiniz Get.dialog() fonksiyonunu sağlar.

Get.dialog(
              AlertDialog(
                title: Text('Merhaba!'),
                content: Text('Dialog kullanımı öğreniyorum.'),
                actions: [
                  TextButton(
                    onPressed: () => Get.back(),
                    child: Text('Kapat'),
                  ),
                ],
              ),
);

Örneğin, bir butona tıklama işlemi sonrası bir dialog göstermek istediğinizi varsayalım. İşte bu senaryo için bir örnek kod:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dialog Kullanımı'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Get.dialog(
              AlertDialog(
                title: Text('Merhaba!'),
                content: Text('Dialog kullanımı öğreniyorum.'),
                actions: [
                  TextButton(
                    onPressed: () => Get.back(),
                    child: Text('Kapat'),
                  ),
                ],
              ),
            );
          },
          child: Text('Dialog Göster'),
        ),
      ),
    );
  }
}

Flutter – Alert Dialog

Flutter’da alert dialog, kullanıcıya önemli bilgileri göstermek ve kullanıcının onayını almadan önce uygulamanın kullanımını durdurmak için kullanılan bir pop-up mesajıdır.

Alert dialogu gösteren bir fonksiyon oluşturun

  void _showAlertDialog(BuildContext context) {
    
    // Alert dialogu oluşturun
    AlertDialog alert = AlertDialog(
      title: const Text("Alert Dialog Başlığı"),
      content: const Text("Bu bir alert dialog örneğidir."),
      actions: [
        TextButton(
          child: const Text("Tamam"),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );

    // Alert dialogu gösterin
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

Stateless widget içerisinde kullanımı

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
          title: const Text('Alert Dialog Örneği'),
        ),
        body: Container(
          color: Colors.lightBlue.shade200,
          padding: const EdgeInsets.only(top: 10),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () => _showAlertDialog(context),
                child: const Text("Göster"),
              )
            ],
          ),
        ));
  }

  // Alert dialogu gösteren bir fonksiyon oluşturun
  void _showAlertDialog(BuildContext context) {
    // Alert dialogu oluşturun
    AlertDialog alert = AlertDialog(
      title: const Text("Alert Dialog Başlığı"),
      content: const Text("Bu bir alert dialog örneğidir."),
      actions: [
        TextButton(
          child: const Text("Tamam"),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ],
    );

    // Alert dialogu gösterin
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }
}