avatarSuragch

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3096

Abstract

span></pre></div><div id="a6d6"><pre> <span class="hljs-comment">// set up the AlertDialog</span> AlertDialog <span class="hljs-attr">alert</span> <span class="hljs-operator">=</span> AlertDialog( <span class="hljs-symbol"> title:</span> Text(<span class="hljs-string">"AlertDialog"</span>), <span class="hljs-symbol"> content:</span> Text(<span class="hljs-string">"Would you like to continue learning how to use Flutter alerts?"</span>), <span class="hljs-symbol"> actions:</span> [ cancelButton, continueButton, ], )<span class="hljs-punctuation">;</span></pre></div><div id="4ecd"><pre> // <span class="hljs-built_in">show</span> the dialog showDialog( <span class="hljs-built_in">context</span>: <span class="hljs-built_in">context</span>, builder: (BuildContext <span class="hljs-built_in">context</span>) { <span class="hljs-built_in">return</span> alert; }, ); }</pre></div><h1 id="926c">Three Buttons</h1><figure id="1e09"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*2mtCwkQpU6fjoReq.png"><figcaption></figcaption></figure><div id="4bf5"><pre><span class="hljs-function"><span class="hljs-title">showAlertDialog</span><span class="hljs-params">(BuildContext context)</span></span> {</pre></div><div id="89f5"><pre> // set up the buttons Widget remindButton = FlatButton( <span class="hljs-name">child</span>: Text(<span class="hljs-string">"Remind me later"</span>), onPressed: () {}, )<span class="hljs-comment">;</span> Widget cancelButton = FlatButton( <span class="hljs-name">child</span>: Text(<span class="hljs-string">"Cancel"</span>), onPressed: () {}, )<span class="hljs-comment">;</span> Widget launchButton = FlatButton( <span class="hljs-name">child</span>: Text(<span class="hljs-string">"Launch missile"</span>), onPressed: () {}, )<span class="hljs-comment">;</span></pre></div><div id="d4d0"><pre> <span class="hljs-comment">// set up the AlertDialog</span> AlertDialog <span class="hljs-attr">alert</span> <span class="hljs-operator">=</span> AlertDialog( <span class="hljs-symbol"> title:</span> Text(<span class="hljs-string">"Notice"</span>), <span class="hljs-symbol"> content:</span> Text(<span class="hljs-string">"Launching this missile will destroy the entire universe. Is this what you intended to do?"</span>), <span class="hljs-symbol"> actions:</span> [ remindButton, cancelButton, launchButton, ], )<span class="hljs-punctuation">;</span></pre></div><div id="7913"><pre> // <span class="hljs-built_in">show</span> the dialog showDialog( <span class="hljs-built_in">context</span>: <span class="hljs-built_in">context</span>, builder: (BuildContext <span class="hljs-built_in">context</span>) { <span class="hljs-built_in">return</span> alert; }, ); }</pre></div><h1 id="73e2">Handling button presses</h1><p id="4e83">The <code>onPressed</code> callback for the buttons in the examples above were empty, but you could add something like this:</p><div id="55c4"><pre>Widget launchButto

Options

n = FlatButton( <span class="hljs-name">child</span>: Text(<span class="hljs-string">"Launch missile"</span>), onPressed: () { Navigator.of(<span class="hljs-name">context</span>).pop()<span class="hljs-comment">; // dismiss dialog</span> launchMissile()<span class="hljs-comment">;</span> }, )<span class="hljs-comment">;</span></pre></div><p id="b561">If you make the callback <code>null</code>, then the button will be disabled.</p><div id="9f96"><pre><span class="hljs-attr">onPressed:</span> <span class="hljs-literal">null</span><span class="hljs-string">,</span></pre></div><figure id="2dc3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*48J-9Yw34LNWwvhW.png"><figcaption></figcaption></figure><h1 id="b0c1">Supplemental code</h1><p id="c9b2">Here is the code for <code>main.dart</code> in case you weren't getting the functions above to run.</p><div id="2941"><pre><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;</pre></div><div id="076c"><pre><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span>()</span> => runApp(MyApp());</pre></div><div id="5edf"><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyApp</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{ <span class="hljs-meta">@override</span> <span class="hljs-type">Widget</span> build(<span class="hljs-type">BuildContext</span> context) { <span class="hljs-keyword">return</span> <span class="hljs-type">MaterialApp</span>( title: '<span class="hljs-type">Flutter</span>', home: <span class="hljs-type">Scaffold</span>( appBar: <span class="hljs-type">AppBar</span>( title: <span class="hljs-type">Text</span>('<span class="hljs-type">Flutter</span>'), ), body: <span class="hljs-type">MyLayout</span>()), ); } }</pre></div><div id="ec39"><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyLayout</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">StatelessWidget</span> </span>{ <span class="hljs-meta">@override</span> <span class="hljs-type">Widget</span> build(<span class="hljs-type">BuildContext</span> context) { <span class="hljs-keyword">return</span> <span class="hljs-type">Padding</span>( padding: const <span class="hljs-type">EdgeInsets</span>.all(<span class="hljs-number">8.0</span>), child: <span class="hljs-type">RaisedButton</span>( child: <span class="hljs-type">Text</span>('<span class="hljs-type">Show</span> alert'), onPressed: () { showAlertDialog(context); }, ), ); } }</pre></div><div id="4393"><pre><span class="hljs-comment">// replace this function with the examples above</span> <span class="hljs-function"><span class="hljs-title">showAlertDialog</span><span class="hljs-params">(BuildContext context)</span></span> { ... }</pre></div></article></body>

Making an AlertDialog in Flutter

This is a repost of an answer I wrote on Stack Overflow.

If you need to display a message to a user and give them time to read it or request a response from them, you can use an AlertDialog. This is the equivalent of an Android AlertDialog or an iOS UIAlertController. The examples below show a basic setup for one, two, and three buttons.

One Button

showAlertDialog(BuildContext context) {
  // set up the button
  Widget okButton = FlatButton(
    child: Text("OK"),
    onPressed: () { },
  );
  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("My title"),
    content: Text("This is my message."),
    actions: [
      okButton,
    ],
  );
  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

Two Buttons

showAlertDialog(BuildContext context) {
  // set up the buttons
  Widget cancelButton = FlatButton(
    child: Text("Cancel"),
    onPressed:  () {},
  );
  Widget continueButton = FlatButton(
    child: Text("Continue"),
    onPressed:  () {},
  );
  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("AlertDialog"),
    content: Text("Would you like to continue learning how to use Flutter alerts?"),
    actions: [
      cancelButton,
      continueButton,
    ],
  );
  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

Three Buttons

showAlertDialog(BuildContext context) {
  // set up the buttons
  Widget remindButton = FlatButton(
    child: Text("Remind me later"),
    onPressed:  () {},
  );
  Widget cancelButton = FlatButton(
    child: Text("Cancel"),
    onPressed:  () {},
  );
  Widget launchButton = FlatButton(
    child: Text("Launch missile"),
    onPressed:  () {},
  );
  // set up the AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("Notice"),
    content: Text("Launching this missile will destroy the entire universe. Is this what you intended to do?"),
    actions: [
      remindButton,
      cancelButton,
      launchButton,
    ],
  );
  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

Handling button presses

The onPressed callback for the buttons in the examples above were empty, but you could add something like this:

Widget launchButton = FlatButton(
  child: Text("Launch missile"),
  onPressed:  () {
    Navigator.of(context).pop(); // dismiss dialog
    launchMissile();
  },
);

If you make the callback null, then the button will be disabled.

onPressed: null,

Supplemental code

Here is the code for main.dart in case you weren't getting the functions above to run.

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter'),
        ),
        body: MyLayout()),
    );
  }
}
class MyLayout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: RaisedButton(
        child: Text('Show alert'),
        onPressed: () {
          showAlertDialog(context);
        },
      ),
    );
  }
}
// replace this function with the examples above
showAlertDialog(BuildContext context) { ... }
Flutter
Alertdialog
Recommended from ReadMedium