I tried everything to show gallery images in grid view in a flutter. But I couldn't find any appropriate method.
5 Answers
What your trying to achieve is pretty complex(folder location,reading the files stored there, identifying the once you would like to display...)
There are two options:
Use this plugin if your not making an app specifically to access photos stored in the users phone without having your own UI
https://pub.dev/packages/image_picker
If your developing an app specifically for accessing photos stored in the user's phone (with own UI)
The below links might help you:
https://pub.dev/packages/image_gallery
https://pub.dev/packages/photo_view
There is even a widget for displaying mutliple widgets in the form of a grid called as GridView.
I would recommend breaking your problem into parts and then ask each of them individually in the form of multiple questions because your basically in a way asking an entire model for an app which would require a lot of explanation.
-
The problem with image_gallery is, that it is exceeding memory, because it's loading ALL Images at once! No broadcasting or streaming of MethodChannel! I definetly don't recommend it!– RebarCommented Mar 24, 2020 at 15:24
You can create a list of widgets and add it in your gridview like this :
List<Widget> images = new List<Widget>();
images.add(Image.asset('assets/image.png', height: 35));
images.add(Image.asset('assets/image.png', height: 35));
images.add(Image.asset('assets/image.png', height: 35));
CustomScrollView(
primary: false,
slivers: <Widget>[
SliverPadding(
padding: const EdgeInsets.all(3.0),
sliver: SliverGrid.count(
mainAxisSpacing: 1, //horizontal space
crossAxisSpacing: 1, //vertical space
crossAxisCount: 3, //number of images for a row
children: images
),
),
],
),
-
I have to add images from the gallery. Images are not in my assets. Commented Mar 22, 2019 at 5:36
-
Oops, sorry my bad. Did you try to make the list with this plugin ? pub.dartlang.org/packages/flutter_multiple_image_picker Commented Mar 22, 2019 at 8:05
Example on this link https://github.com/follow2vivek/FlutterGallery
https://github.com/follow2vivek/FlutterGallery/blob/master/lib/tabview/image.dart
@override
Widget build(BuildContext context)
{
return FutureBuilder(
future: StoragePath.imagesPath,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<dynamic> list = json.decode(snapshot.data);
return Container(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
ImageModel imageModel = ImageModel.fromJson(list[index]);
return Container(
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
FadeInImage(
image: FileImage(
File(imageModel.files[0]),
),
placeholder: MemoryImage(kTransparentImage),
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
Container(
color: Colors.black.withOpacity(0.7),
height: 30,
width: double.infinity,
child: Center(
child: Text(
imageModel.folderName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontFamily: 'Regular'
),
),
),
)
],
),
);
},
),
);
} else {
return Container();
}
},
);
}
step 1: import package on yaml file "gallery_view: ^0.0.4" step 2: pub get (install package) step 4: add images as url or from asset images step 3: finally run the code b
import 'package:flutter/material.dart';
import 'package:gallery_view/gallery_view.dart';
void main() {
runApp(GalleryGridScreen());
}
class GalleryGridScreen extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<GalleryGridScreen> {
String _platformVersion = 'Unknown';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Gallery'),
),
body: GalleryView(
crossAxisCount: 2,
imageUrlList: [
"imageurl1",
"imageurl2",
"imageurl3",
"imageurl4",
]),
),
);
}
}