Flutter App is getting crashed when picking multiple images from gallery and displaying on to the screen. Is there a way to compress the pictures before displaying them? How to compress multiple pictures at a time? Can we display images as thumbnails to load faster and smoother?
I am currently using image picker to pick multiple images and store them in a list. How to compress all the selected pictures in the list and display them without crashing? Please help me out!
List<XFile> picturesList = [];
pickImages() async {
try {
final List<XFile>? pickedImages = await ImagePicker().pickMultiImage();
if (pickedImages!.isNotEmpty) {
picturesList.addAll(pickedImages);
notifyListeners();
}
} catch (e) {
debugPrint(e.toString());
}
}
class ImagesGrid extends StatelessWidget {
const ImagesGrid({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
ElevatedButton(
onPressed: () {
pickImages();
},
child: const Text('Pick Images')),
SizedBox(
height: 600,
child: GridView.builder(
padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
mainAxisSpacing: 5.0,
crossAxisSpacing: 5.0,
crossAxisCount: 4),
itemCount: picturesList.length,
itemBuilder: (context, index) {
return SizedBox(
width: 100,
height: 100,
child: Image.file(
File(picturesList[index].path),
fit: BoxFit.cover,
),
);
},
),
),
],
);
}
}