gpt4 book ai didi

user-interface - Flutter - 如何在 AlertDialog 框中显示 GridView?

转载 作者:IT王子 更新时间:2023-10-29 06:50:11 31 4
gpt4 key购买 nike

我正在尝试显示图像的 GridView 以允许用户从对话框中进行选择,但我遇到了呈现问题。我的 UI 逐渐消失,就好像一个 View 出现在它上面一样,但没有任何显示。这是我的代码...

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();
})
],
),
);
}

最佳答案

问题是,AlertDailog 试图获取子项的固有宽度。但是 GridView 是惰性的,不提供内在属性。只需尝试将 GridView 包装在 Container 中,并设置一些 width

例子:

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();
})
],
),
);
}

您可以阅读更多有关如何为不同 View 计算内在属性的信息 here .

希望对您有所帮助!

关于user-interface - Flutter - 如何在 AlertDialog 框中显示 GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375050/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com