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;
      },
    );
  }
}