Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display all images in a directory to a list in Flutter

I was wondering if there is a way i can display all images or files in a particular directory located in user's mobile device to a list/array in flutter so i can use in a ListView.

Thanks any help is welcomed.

PS: i wanted to try with path_provider but didn't know how to.

like image 322
JideGuru Avatar asked Feb 14 '19 13:02

JideGuru


People also ask

How do I display list of images in flutter?

and append the image. Step 4: Now in the body of the scaffold, Create a ListView widget, In ListView first, we will use the row to show the item count and a button. Now add one more child to the listview, which will show the list of images.

How do I list files in a directory in flutter?

To list all the files or folders, you have to use flutter_file_manager, path, and path_provider_ex flutter package. Add the following lines in your pubspec. yaml file to add this package in your dependency. Add read / write permissions in your android/app/src/main/AndroidManifest.


1 Answers

I am working on application that uses local storage / application directory.

After spending five days I got a solution...

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

final Directory _photoDir = new Directory(
    '/storage/emulated/0/Android/data/com.eclixtech.doc_scanner/files/CroppedImages');

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Document Scanner',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ImageCapture(),
    );
  }
}

class ImageCapture extends StatefulWidget {
  @override
   _ImageCaptureState createState() => _ImageCaptureState();
}

class _ImageCaptureState extends State<ImageCapture> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Doc Scanner'),
      ),
      body: Container(
        child: FutureBuilder(
          builder: (context, status) {
            return ImageGrid(directory: _photoDir);
          },
        ),
      ),
    );
  }
}
class ImageGrid extends StatelessWidget {
  final Directory directory;

  const ImageGrid({Key key, this.directory}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var refreshGridView;
    var imageList = directory
        .listSync()
        .map((item) => item.path)
        .where((item) => item.endsWith(".jpg"))
        .toList(growable: false);
    return GridView.builder(
      itemCount: imageList.length,
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3, childAspectRatio: 3.0 / 4.6),
      itemBuilder: (context, index) {
        File file = new File(imageList[index]);
        String name = file.path.split('/').last;
        return Card(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8.0),
          ),
          child: Padding(
            padding: const EdgeInsets.all(5.0),
            child: InkWell(
              onTap: () => {
                refreshGridView =
                Navigator.push(context, MaterialPageRoute(builder: (context) {
              return ImageEditClass(
                imagepath: imageList[index],
              );
            })).then((refreshGridView) {
              if (refreshGridView != null) {
                build(context);
              }
            }).catchError((er) {
              print(er);
            }),
              },
               child: Padding(
                 padding: new EdgeInsets.all(4.0),
                 child: Image.file(
                   File(imageList[index]),
                   fit: BoxFit.cover,
                 ),
               ),
            ),
          ),
        );
      },
    );
  }
}

You can mention the extention(.jpg, .png ect) of images here.

.where((item) => item.endsWith(".jpg"))

in above mentioned code.

like image 101
Abdul Rauf Avatar answered Sep 30 '22 08:09

Abdul Rauf