Reputation: 1009
Currently dialog show in center of the screen. I want to show dialog just after the add icon.
Code :
Widget build(BuildContext context) {
return Scaffold(
body: ListTile(
leading: IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
await showDialogOnButtonPress();
},
),
title: const Text("Title 1"),
)
);
}
showDialogOnButtonPress() Function :
showDialogOnButtonPress() {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const Dialog(
child: ListTile(
dense: true,
title: Text(
"Alert!",
),
),
);});}
Thanks.
Upvotes: 1
Views: 4646
Reputation: 53
Remember Dialog Widget is center aligned it is not meant to be customized.
Even if you want custom position then use stack and positioned widget to achieve the custom results.
// Replace Dialog widget with below code for custom result.
Stack(
children: [
Positioned(
top: MediaQuery.of(context).size.height * 0.50,
child: => your ListTile widget goes here.
),
],
)
Upvotes: 0
Reputation: 11
You can align and position your dialog widget like this.
showDialog(
context: context,
builder: (context) {
return Align(
alignment: Alignment.topCenter,
child: Container(
width: 100,
child: ListTile(
title: Text("Alert!"),
),
),
);
});
Upvotes: 0
Reputation: 325
Maybe you can use Overlay
instead of Dialog
.
late OverlayEntry _popupDialog;
void _showPopupDialog(int titleIndex) {
_popupDialog = _buildPopupDialog(titleIndex);
Overlay.of(context)?.insert(_popupDialog);
}
void _dismissPopupDialog() {
_popupDialog.remove();
}
OverlayEntry _buildPopupDialog(int index) {
RenderBox renderBox = ...; // find the title, as the anchor view of popupDialog
final size = renderBox.size;
final offset = renderBox.localToGlobal(Offset.zero);
return OverlayEntry(
builder: (context) => Positioned(
left: offset.dx,
top: offset.dy + size.height,
child: Material( // make it seems like a dialog
elevation: 8,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
),
child: Container(
width: ...,
height: ...,
child: ...,
),
),
)
);
}
And if your dialog will show a selectable list, DropdownButton
with DropdownMenuItem
is better.
Upvotes: 1
Reputation: 7709
You can align and position your dialog like this.
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return Dialog(
alignment: Alignment.topLeft,
insetPadding: EdgeInsets.only(left: 5, top: 20),
child: Container(
width: 100,
child: ListTile(
title: Text("Alert!"),
),
),
);
},
);
Upvotes: 2
Reputation: 14885
Try below code set alignment: Alignment.topLeft,
:
showDialogOnButtonPress(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const Dialog(
alignment: Alignment.topLeft,
child: ListTile(
dense: true,
title: Text(
"Alert!",
),
),
);
});
}
Widget:
ListTile(
leading: IconButton(
icon: const Icon(Icons.add),
onPressed: () async {
await showDialogOnButtonPress(context);
},
),
title: const Text("Title 1"),
),
Upvotes: 3
Reputation: 1
I propose to you to use a visibility widget next to this widget and Change the State of visible to true whee it's successfully added.
Upvotes: 0