Master Flutter Dialog Boxes: A Guide to Building Interactive Experiences
Master Flutter Dialog Boxes: A Guide to Building Interactive Experiences
Dialogs are a cornerstone of modern mobile app design, enabling seamless user interactions such as feedback submission, information requests, and action prompts. Flutter's flexibility makes creating custom dialogs a breeze, offering you the power to craft unique user experiences. In this guide, we'll explore the creation of different types of dialogs, culminating in a fully customized dialog for task addition.
Understanding Flutter Dialogs
Flutter provides several built-in dialog widgets, including:
- AlertDialog: Ideal for displaying simple messages or prompts.
- SimpleDialog: Great for presenting options or menus.
- Custom Dialogs: Perfect for personalized user interfaces.
1. AlertDialog
The AlertDialog
widget is the simplest form of dialog in Flutter. It is used to:
- Display important information
- Confirm user actions with a simple “OK” or “Cancel” response
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Dialog Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Item'),
content: Text('Are you sure you want to delete this item?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
TextButton(
onPressed: () {
// Perform delete operation
print('Item deleted');
Navigator.pop(context);
},
child: Text('Delete'),
),
],
);
},
);
},
child: Text('Show Dialog'),
),
),
);
}
}
2. SimpleDialog
The SimpleDialog
widget is used to:
- Present users with a list of options
- Allow users to select one option from a list
body: Center(
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text('Choose an Option'),
children: [
SimpleDialogOption(
onPressed: () {
// Handle option 1
Navigator.pop(context);
},
child: Text('Option 1'),
),
SimpleDialogOption(
onPressed: () {
// Handle option 2
Navigator.pop(context);
},
child: Text('Option 2'),
),
],
);
},
);
},
child: Text('Show Dialog'),
),
),
SimpleDialog |
- Title: The header text of the dialog.
- Children: The list of options presented to the user.
3. 🛠️ Custom Dialog
When the default dialogs aren't enough, you can create fully customized dialogs tailored to your app's design.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Dialog Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
Future<void> showCustomDialog(BuildContext context) async {
final TextEditingController taskController = TextEditingController();
await showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.purple],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(20),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Add New Task',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const SizedBox(height: 15),
TextField(
controller: taskController,
decoration: InputDecoration(
labelText: 'Task Name',
labelStyle: TextStyle(color: Colors.white),
filled: true,
fillColor: Colors.white.withOpacity(0.2),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide.none,
),
),
style: TextStyle(color: Colors.white),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(
'Cancel',
style: TextStyle(color: Colors.white),
),
style: TextButton.styleFrom(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 16,
),
backgroundColor: Colors.red.withOpacity(0.8),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
ElevatedButton(
onPressed: () {
String task = taskController.text.trim();
if (task.isNotEmpty) {
print('Task Added: $task');
Navigator.pop(context);
}
},
child: Text('Add'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(
vertical: 12,
horizontal: 16,
),
backgroundColor: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
],
),
],
),
),
),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dialog Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showCustomDialog(context);
},
child: Text('Show Dialog'),
),
),
);
}
}
| 🏁 Conclusion
Custom dialogs in Flutter unlock endless possibilities for creating interactive and user-friendly interfaces. Whether you need a simple alert or a complex form, Flutter’s powerful widget system allows you to design dialogs that perfectly align with your app's requirements. By following these steps, you can easily integrate custom dialogs into your projects and enhance the overall user experience.
Start experimenting today and take your Flutter applications to the next level!
Start experimenting today and take your Flutter applications to the next level!
Comments
Post a Comment