xxxxxxxxxx
import 'package:flutter/material.dart';
// Function to show the dialog box
void showDialogBox(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Dialog Box"),
content: Text("This is a sample dialog box."),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
// Usage example
class MyExampleScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dialog Box Example"),
),
body: Center(
child: RaisedButton(
child: Text("Show Dialog Box"),
onPressed: () {
showDialogBox(context); // Call the function to show the dialog box
},
),
),
);
}
}
xxxxxxxxxx
void showDialogWithFields() {
showDialog(
context: context,
builder: (_) {
var emailController = TextEditingController();
var messageController = TextEditingController();
return AlertDialog(
title: Text('Contact Us'),
content: ListView(
shrinkWrap: true,
children: [
TextFormField(
controller: emailController,
decoration: InputDecoration(hintText: 'Email'),
),
TextFormField(
controller: messageController,
decoration: InputDecoration(hintText: 'Message'),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
TextButton(
onPressed: () {
// Send them to your email maybe?
var email = emailController.text;
var message = messageController.text;
Navigator.pop(context);
},
child: Text('Send'),
),
],
);
},
);
}
xxxxxxxxxx
showDialog(context: context, builder: (BuildContext context){
return AlertDialog(
title: Text("Success"),
content: Text("Saved successfully"),
);
});
xxxxxxxxxx
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Dialog Title"),
content: Text("Dialog Message"),
actions: [
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
xxxxxxxxxx
Future<void> _showMyDialog() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text('AlertDialog Title'),
content: SingleChildScrollView(
child: Column(
children: <Widget>[
Text('This is a demo alert dialog.'),
Text('Would you like to confirm this message?'),
],
),
),
actions: <Widget>[
TextButton(
child: Text('Confirm'),
onPressed: () {
print('Confirmed');
Navigator.of(context).pop();
},
),
TextButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
xxxxxxxxxx
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Testing")),
body: Center(
child: RaisedButton(
child: Text("Show dialog"),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
elevation: 16,
child: Container(
height: 400.0,
width: 360.0,
child: ListView(
children: <Widget>[
SizedBox(height: 20),
Center(
child: Text(
"Leaderboard",
style: TextStyle(fontSize: 24, color: Colors.blue, fontWeight: FontWeight.bold),
),
),
SizedBox(height: 20),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 1", score: 1000),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 2", score: 2000),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 3", score: 3000),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 4", score: 4000),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 5", score: 5000),
_buildName(imageAsset: 'assets/chocolate.jpg', name: "Name 6", score: 6000),
],
),
),
);
},
);
},
),
),
);
}
Widget _buildName({String imageAsset, String name, double score}) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
children: <Widget>[
SizedBox(height: 12),
Container(height: 2, color: Colors.redAccent),
SizedBox(height: 12),
Row(
children: <Widget>[
CircleAvatar(
backgroundImage: AssetImage(imageAsset),
radius: 30,
),
SizedBox(width: 12),
Text(name),
Spacer(),
Container(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 20),
child: Text("${score}"),
decoration: BoxDecoration(
color: Colors.yellow[900],
borderRadius: BorderRadius.circular(20),
),
),
],
),
],
),
);
}
xxxxxxxxxx
showDialog(
context: context,
builder: (context) => AboutDialog(
applicationName: 'AllAboutFlutter',
applicationVersion: '1.0.0',
applicationIcon: CircleAvatar(
child: Image.network(
'https://static.wixstatic.com/media/73e5ab_e0a33a24b96c4e09a876668a29fd73c4~mv2.png'),
),
applicationLegalese:
'© 2023 AllAboutFlutter. All rights reserved.',
children: const [
Text(
'AllAboutFlutter is a website that provides tutorials on Flutter and Dart.'),
],
),
);
xxxxxxxxxx
static void showDialog({
required String title,
required int messageCode,
required String content,
}) {
Get.defaultDialog(
title: title + " #" + messageCode.toString(),
titleStyle: FontConstants.SFProDisplayBold(fontSize: 24,color: Colors.black),
content: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text(
content,
style: FontConstants.SFProDisplayLight(fontSize: 20, color: Colors.black),
),
),
barrierDismissible: true,
radius: 5,
actions: [
RaisedButton(
onPressed: () {
Get.back();
},
child: Text(
'okay'.tr,
style:
FontConstants.SFProDisplayLight(fontSize: 22, color: ColorConstants.blueBold),
),
color: Colors.white,
),
]);
}