Learn Simple Alert Dialog Box In Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 10-09-2020

alert dialog box is used to show a simple alert message on both android and iOS flutter mobile applications. The alert dialog contains a message as Title and a OK button. The basic requirement of Alert Dialog is when we want to suddenly display a response message coming from API or wants to alert the app using then we have to use Alert Dialog box. In flutter we would use inbuilt showAlert() function to display Alert Dialog box.

Learn Simple Alert Dialog Box In Flutter Android App

The showAlert() method returns a AlertDialog widget with customization options like custom Title text and Buttons. 
Complete Code For Simple Alert Dialog Box In FLutter

main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            appBar: AppBar(
              centerTitle: true,
                backgroundColor: Colors.indigo,
                title: Text('Simple Alert Dialog Box')
            ),
            body: Center(
                child: Alert()
            )
        )
    );
  }
}

class Alert extends StatefulWidget {

  AlertState createState() => AlertState();

}

class AlertState extends State {

  showAlert(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text('Alert Message.'),
          actions: <Widget>[
            FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child:
        RaisedButton(
          onPressed: () => showAlert(context),
          child: Text('Show Alert Dialog BOx'),
          textColor: Colors.white,
          color: Colors.indigo,
          padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
        ),
      ),
    );
  }
}

 

Related Post