Skip to main content

Flutter and Dart Create Dialog Widget

Flutter and Dart provide us a comman tool for developement of the mobile app for Android and IOS platform.  As mobile app developer we often required to implement the Dialog to so quick action, some alert msg and more to the user. In Simple word Dialogs are used to inform users about a task and can contain critical information, require decisions, or involve multiple tasks. So in this post we are going to explore the several type of the Dialoag and its implementation in Flutter. 




Type of Dialog





We have three types of dialog that Flutter and Dart SDK platform provide us.





  • Alert Dialog -- To show some content and action input from the end-user
  • Simple Dialog -- Its have lots customization option to show the dialog
  • About Dialog -- Dialog to show about us with icon and text.




Alert Dialog and its implementation in Flutter and Dart





First of all, we are going to implement the alert dialog. If you are looking to show the dialog then I am sure you have good knowledge about the dart and flutter and you have already a running project, if not then you can refer our earlier post to get started Creating a Cross-platform Mobile App with Flutter and Dart.





Let's create a function and button in main.dart file to show the dialog. Below is a simple function which will show the Alert Dialog and will pop out when the user tab on the action FlatButton.





Future<void> _showDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert Dialog Demo In Flutter'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Alert Dialog!.'),
Text('You are Loving Flutter and Dart, is I am Right?'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Yes I Am'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}




Now, we need to add a button to our home widget view to show the Alert Dialog when user tab on the button.





 RaisedButton(
color: Colors.blue,
child: Text("Show Dialog",style: new TextStyle(fontSize: 24,color: Colors.white),),

onPressed: () {
_showDialog();
},
),








The full example of the Alert Dialog in Flutter





import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Dialog Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {


@override
Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(

title: Text(widget.title),
),
body: Center(

child: Column(

mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[

RaisedButton(
color: Colors.blue,
child: Text("Show Dialog",style: new TextStyle(fontSize: 24,color: Colors.white),),

onPressed: () {
_showDialog();
},
),
],
),
),

);
}



Future<void> _showDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert Dialog Demo In Flutter'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Alert Dialog!.'),
Text('You are Loving Flutter and Dart, is I amRight?'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Yes I Am'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
}




So, we have created an Alert Dialog which will get dismissed as soon as user tab on the "Yes I am" Flat button.





Alert Dialog with Positive and Negative Button in Flutter





Let's create another Alert Dialog which will show the Negative and Positive button both and once the user will tab the option a specific function can run.





enum Option{
positive,
negative
}




.We also need to change our Future _showDailog function as like below. In this dialog, a switch statement is used to decide which option has been pressed.





   Future _showDialog() async {
switch (await showDialog<Option>(

context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert Dialog Demo In Flutter'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Alert Dialog!.'),
Text('You are Loving Flutter and Dart, is I amRight?'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Yes I Am'),
onPressed: () {
/// perform any action
Navigator.of(context).pop(Option.positive);
},
),
FlatButton(
child: Text('No'),
onPressed: () {
/// perform any action
Navigator.of(context).pop(Option.negative);
},
),
],
);
},
)
)
{
case Option.negative:
// Perform Any action
print("${Option.negative}");
break;
case Option.positive:
// Perform Any action
print("${Option.positive}");
break;
}
}




Alert Dialog with Postive and Negative Button




Simple Dialog





Simple Dialog provides the functionality to choose the option from several available options. For example, you have just asked a question to the user 'Which is your Favorite" OS' and provide the list of the OS to choose.





First of all, create our choice for the user using the enum.





enum OS{
Android,
Windows,
IOS,
None
}




Let's create our Simple Dialog as given below.





Future<void> _simpleDialog() async {
switch (await showDialog<OS>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text('Which is your Favourite OS?'),
children: <Widget>[

SimpleDialogOption(
onPressed: () { Navigator.pop(context, OS.Android); },
child: const Text('Android'),
),
SimpleDialogOption(
onPressed: () { Navigator.pop(context, OS.IOS); },
child: const Text('IOS'),
),
SimpleDialogOption(
onPressed: () { Navigator.pop(context, OS.Windows); },
child: const Text('Windows'),
),
SimpleDialogOption(
onPressed: () { Navigator.pop(context, OS.None); },
child: const Text('None of the Above'),
),

],

);
}
)
)
{
case OS.Android:
print("I am Fan of the Android");
break;
case OS.Windows:
print("I am Windows Lover");
break;
case OS.IOS:
print("I Like IOS.}");
break;
case OS.None:
print("None of the above.");
break;
}
}




Now, call this _simpleDailog function from anywhere to show the dialog.





Simple Dialog in Flutter




About Dialog in Flutter





About Dialog are used to show the details of the application and used software and platform to develop the application. Its include the name, icon, etc and one view license button. So let's create a function to show the about dialog in Flutter.





Future<void> _showAboutDialog() async{
showAboutDialog(
context: context,
applicationName: "Dialog Demo",
applicationVersion: "1.2.1",
applicationIcon: new Icon(Icons.android,color: Colors.green,),
applicationLegalese: "www.nplix.com",
children: <Widget> [
Text("Welcome to NPLIX.com"),
],
);
}




About Dialog




When you tap on the View Licenses it will open the license details in new screen like given below.





About License Dialog




Wrapping Up





So in this post, we have created three types of dialog in flutter Alert Dialog, Simple Dialog and About us Dialog. If have any query on this please ask in the comment section. You can further learn more about these on the Flutter page.


Comments

Popular posts from this blog

WordPress Android App with REST API And Retrofit

Retrofit is the library which converts your JSON data into Java object. In this tutorial, we will learn about how to implement the Retrofit to create a WordPress Mobile App. As we all know, WordPress is the one of the biggest CMS (Content Management System) of the world. Wordpress full fill the requirements for all most every type of the website. So, in this tutorial, I am going to learn how to create an Android App using the WordPress REST API and Retrofit.
What is Retrofit?
If you want to know about Retrofit, I would like to give you just a simple one-line explanation of the Retrofit. It is a library which converts your REST HTTP request to Java interface. We will learn how we can do all these stuff using some very simple steps.

Further, you can check the Retrofit Library here.

So, In this tutorial, we will use the Retrofit to retrieve all post and there except ( A short Description of Post) form a WordPress website.
Prerequisite for using the Retrofit for WordPress Android App
There is s…

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View.

Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android
Just go to the android folder and open it in separate windows. We will have the following project structure.









Create the Activity Just right click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project sync completed.
Update the project build config …

WP Android App using REST and volley

In this tutorial, we are going to create a sample app for WordPress using the rest API for Android. For successful implementation of the rest API for WordPress we need to it in two parts. In the first part, we will learn some basic configuration on the server side and configure the Android project in android using the Android Studio.


WP Android App using REST and volley part1
WP Android App using REST and volley part2
LiveData and ViewModel for WP Android App using REST and volley part3


Requirement to create the Android App for WordPress


WordPress API is included in core release of the WordPress version 4.7 and above.
For older version, you need to install the WordPress plugin API
Android Studio 2.X.X or 3.X.X


If you are using the WordPress version 4.7 and above you do not require any special configuration for this, you can just test your configuration by sending the sample request to the  http://yoursite/wp-json/ . You will get the output into the JSON format.

Now, If you are using the older …