Flutter - How to display a GridView within an AlertDialog box?
The issue is that, the AlertDailog
tries to get the intrinsic width of the child. But GridView
being lazy does not provide intrinsic properties. Just try wrapping the GridView
in a Container
with some width
.
Example:
Future<Null> _neverSatisfied() async {
return showDialog<Null>(
context: context,
barrierDismissible: false, // user must tap button!
child: new AlertDialog(
contentPadding: const EdgeInsets.all(10.0),
title: new Text(
'SAVED !!!',
style:
new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
content: new Container(
// Specify some width
width: MediaQuery.of(context).size.width * .7,
child: new GridView.count(
crossAxisCount: 4,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
children: <String>[
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
'http://www.for-example.org/img/main/forexamplelogo.png',
].map((String url) {
return new GridTile(
child: new Image.network(url, fit: BoxFit.cover, width: 12.0, height: 12.0,));
}).toList()),
),
actions: <Widget>[
new IconButton(
splashColor: Colors.green,
icon: new Icon(
Icons.done,
color: Colors.blue,
),
onPressed: () {
Navigator.of(context).pop();
})
],
),
);
}
You can read more about how intrinsic properties are calculated for different views here.
Hope that helps!
Charles Jr
Jack of All Trades - Master of Few!! Started with Obj-C.then(Swift)!.now(Flutter/Dart)! Thank everyone for every answer, comment, edit, suggestion. Can't believe I've been able to answer a few myself :-)
Updated on December 04, 2022Comments
-
Charles Jr over 1 year
I'm trying to display a GridView of images to allow a user to pick from within a Dialog Box and I'm having rendering issues. My UI fades as if a view was appearing on top of it, but nothing displays. Here is my code...
Future<Null> _neverSatisfied() async { return showDialog<Null>( context: context, barrierDismissible: false, // user must tap button! child: new AlertDialog( title: new Text( 'SAVED !!!', style: new TextStyle(fontWeight: FontWeight.bold, color: Colors.black), ), content: new GridView.count( crossAxisCount: 4, childAspectRatio: 1.0, padding: const EdgeInsets.all(4.0), mainAxisSpacing: 4.0, crossAxisSpacing: 4.0, children: <String>[ 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', ].map((String url) { return new GridTile( child: new Image.network(url, fit: BoxFit.cover, width: 12.0, height: 12.0,)); }).toList()), actions: <Widget>[ new IconButton( splashColor: Colors.green, icon: new Icon( Icons.done, color: Colors.blue, ), onPressed: () { Navigator.of(context).pop(); }) ], ), ); }
-
SlimenTN over 5 yearsWorks fine, thank you! But can you explain what
MediaQuery.of(context).size.width * .7
does exactly ? -
Hemanth Raj over 5 yearsThe
MediaQuery
provides the size property which holds the width and height of the screen. By doing* .7
on it, I'm getting 70% of width. -
LuckyLizard over 4 yearsNice, it also helped with nesting
Scrollbar
withMarkdown
widget intoAlertDialog
. I was sure wrapping it withContainer
would help, but setting the size based on screen dimensions wasn't that obvious. Thanks!