flutter-tutorial

Flutter AlertDialog

The Alert Dialog box warns the user of a problem that requires their attention. An alert box is a prompt that asks the user to confirm something. When we close an app, we are frequently prompted whether we want to depart or not, which is the most basic use of the alert box. That’s a warning box.

Alter Dialog Box’s Most Important Features:

  • actions: The list of actions displayed at the bottom of the dialogue title is as follows: At the top of the dialogue, the dialog’s title is shown in huge font.
  • content: This provides a message/description for the title you entered into the Alert Dialog box.
  • backgroundColor: This property specifies the colour of the widget’s background.
  • elevation: Elevation gives the widget a default shadow and provides it with height.

Example:

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Learn Code Zone',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyView(),
    );
  }
}
class MyView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter AlertDialog'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Alert Dialog'),
          onPressed: () {
            _showDialog(context);
          },
        ),
      ),
    );
  }
}
void _showDialog(BuildContext context) {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text("Alert!!"),
        content: new Text("You are awesome!"),
        actions: <Widget>[
          new FlatButton(
            child: new Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published.