【发布时间】:2021-03-24 09:42:46
【问题描述】:
我将如何继续从我的 GridView 对象中删除图像,长按会打开一个 PopupMenu,在其中我可以选择删除该照片?
到目前为止我的代码:
Widget gridView(){
return GridView.count(
crossAxisCount: 3,
children: List.generate(images.length + localFiles.length, (index) {
if(index < localFiles.length){
return GestureDetector(
onLongPress: () {
print("Hello");
},
child: Container(
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
color: Colors.black12,
image: DecorationImage(
image: FileImage(File(localFiles[index])), fit: BoxFit.cover,
),
),
)
);
}
return GestureDetector(
onLongPress: () {
print("Hello");
},
child: Container(
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
color: Colors.black12,
image: DecorationImage(
image: NetworkImage(images[index-localFiles.length]), fit: BoxFit.cover,
),
),
)
);
}),
);
我的最终代码适用于需要找到类似问题的解决方案的任何人: (另外这里是来自包创建者的 FocusedMenuHolder 包的一个很好的资源来帮助你https://www.youtube.com/watch?v=Y2XyyJcSTQo)
return GridView.count(
crossAxisCount: 3,
physics: BouncingScrollPhysics(),
padding: const EdgeInsets.fromLTRB(1, 0, 1, 0),
children: List.generate(images.length + localFiles.length, (index) {
if(index < localFiles.length){
return FocusedMenuHolder(
blurSize: 2,
blurBackgroundColor: Colors.black12,
menuWidth: 200,
duration: Duration(milliseconds: 200),
onPressed: (){},
menuItems: <FocusedMenuItem>[
FocusedMenuItem(title: Text("Share"),trailingIcon: Icon(Icons.share) ,onPressed: (){
}),
FocusedMenuItem(title: Text("Delete",style: TextStyle(color: Colors.redAccent),),trailingIcon: Icon(Icons.delete,color: Colors.redAccent,) ,onPressed: (){}),
],
child: Container(
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
color: Colors.black12,
image: DecorationImage(
image: FileImage(File(localFiles[index])), fit: BoxFit.cover,
),
),
),
);
}
return Container(
margin: const EdgeInsets.all(1.0),
decoration: BoxDecoration(
color: Colors.black12,
image: DecorationImage(
image: NetworkImage(images[index-localFiles.length]), fit: BoxFit.cover,
),
),
);
}),
);
【问题讨论】:
标签: flutter dart flutter-layout