Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Custom dialog box in flutter?

I'm new to flutter and need to create a gallery app that needs a custom dialog box to show the selected image. How can I implement that?

like image 817
Ajnas Askar Avatar asked Oct 27 '18 05:10

Ajnas Askar


People also ask

How do you create a custom dialog box in Flutter?

Adding simple Dialog to your screen in pretty easy in Flutter. Before adding Dialog you must call showDialog function to change current screen state to show the intermediate Dialog popup. In here we use AlertDialog widget to show simple Dialog with title and some text in the body.

How do you open the dialog box in Flutter?

We need the function "showDialog()" to display the Material Dialog on the Page. In Flutter everything is a Widget. So the Dialog is also a Material Design Widget.

How do you align dialog boxes in Flutter?

You can Use Align widget and align your dialog widget as per your need. Here in example i am setting it to the bottomCenter that is Alignment(0, 1) . Example code: Align( alignment: Alignment(0, 1), child: Material( shape: RoundedRectangleBorder(borderRadius: BorderRadius.

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.


2 Answers

Use Dialog class which is a parent class to AlertDialog class in Flutter. Dialog widget has a argument , "shape" which you can use to shape the Edges of the Dialog box.

Here is a code sample:

 Dialog errorDialog = Dialog(   shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)), //this right here   child: Container(     height: 300.0,     width: 300.0,         child: Column(       mainAxisAlignment: MainAxisAlignment.center,       children: <Widget>[         Padding(           padding:  EdgeInsets.all(15.0),           child: Text('Cool', style: TextStyle(color: Colors.red),),         ),         Padding(           padding: EdgeInsets.all(15.0),           child: Text('Awesome', style: TextStyle(color: Colors.red),),         ),         Padding(padding: EdgeInsets.only(top: 50.0)),         TextButton(onPressed: () {           Navigator.of(context).pop();         },             child: Text('Got It!', style: TextStyle(color: Colors.purple, fontSize: 18.0),))       ],     ),   ), ); showDialog(context: context, builder: (BuildContext context) => errorDialog);} 
like image 117
Pratik Jain Avatar answered Sep 23 '22 19:09

Pratik Jain


Screenshot (Null Safe):

enter image description here


Code:

Just call this method:

void showCustomDialog(BuildContext context) {   showGeneralDialog(     context: context,     barrierLabel: "Barrier",     barrierDismissible: true,     barrierColor: Colors.black.withOpacity(0.5),     transitionDuration: Duration(milliseconds: 700),     pageBuilder: (_, __, ___) {       return Center(         child: Container(           height: 240,           child: SizedBox.expand(child: FlutterLogo()),           margin: EdgeInsets.symmetric(horizontal: 20),           decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40)),         ),       );     },     transitionBuilder: (_, anim, __, child) {       Tween<Offset> tween;       if (anim.status == AnimationStatus.reverse) {         tween = Tween(begin: Offset(-1, 0), end: Offset.zero);       } else {         tween = Tween(begin: Offset(1, 0), end: Offset.zero);       }          return SlideTransition(         position: tween.animate(anim),         child: FadeTransition(           opacity: anim,           child: child,         ),       );     },   ); } 
like image 29
CopsOnRoad Avatar answered Sep 21 '22 19:09

CopsOnRoad