Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style AlertDialog Actions in Flutter

I use this method to show a AlertDialog:

_onSubmit(message) {     if (message.isNotEmpty) {       showDialog(         context: context,         barrierDismissible: false,         builder: (BuildContext context) {           return AlertDialog(             title: Center(child: Text('Alert')),             content: Row(               mainAxisAlignment: MainAxisAlignment.center,               crossAxisAlignment: CrossAxisAlignment.center,               children : <Widget>[                 Expanded(                   child: Text(                     message,                     textAlign: TextAlign.center,                     style: TextStyle(                       color: Colors.red,                      ),                   ),                 )               ],             ),             actions: <Widget>[               FlatButton(                   child: Text('Cancel'),                   onPressed: () {                     Navigator.of(context).pop();                   }),               FlatButton(                   child: Text('Ok'),                   onPressed: () {                     _inputTextController.clear();                     Navigator.of(context).pop();                   })             ],           );         },       );     }   } 

Everything is working but the buttons are aligned in right as shown on picture below:

enter image description here

I want to style some how the buttons, for example one on start other on end. I searched in docs but only found how to make them "Stacked full-width buttons". Any ideas how to style the buttons?

like image 785
MeLean Avatar asked Jul 08 '18 18:07

MeLean


People also ask

How do you style AlertDialog in Flutter?

Edit the the widget itself: Under the AlertDialog there is a ButtonBar widget where you can use alignment: MainAxisAlignment. spaceBetween to align the buttons correctly. See this answer for an example of a custom AlertDialog widget. Show activity on this post.

How do I customize alert dialog Flutter?

Flutter custom Alert Dialog If you want to implement more advanced custom Dialog, you can use Dialog widget for that. Instead of the AlertDialog , in here we return Dialog widget. The showDialog method will remain the same. You can use a Container widget to set relevant height for the Dialog.

How do I show AlertDialog in Flutter?

Create a Flutter project in Android Studio and replace the following code with main. dart file. To show an alert, you must have to call showDialog() function, which contains the context and itemBuilder function. The itemBuilder function returns an object of type dialog, the AlertDialog.


1 Answers

Customize widget

Edit the the widget itself: Under the AlertDialog there is a ButtonBar widget where you can use alignment: MainAxisAlignment.spaceBetween to align the buttons correctly. See this answer for an example of a custom AlertDialog widget.

Own button row

You could also remove the buttons under actions and add an own custom Row with RaisedButtons in it, somehow like this:

Row (     mainAxisAlignment: MainAxisAlignment.spaceBetween,     children: <Widget>[         RaisedButton(), // button 1         RaisedButton(), // button 2     ] ) 

In your case you could add a Column around the Row in content and in there add your existing Row and the modified one you created from the above example.

like image 137
Bostrot Avatar answered Sep 29 '22 10:09

Bostrot