flutterwidgetphoto

Open photos in Flutter


In my Flutter app I'm showing some images, currently when I click on them I can download them but I wanted to change that, so that when I click on the image it opens, does anyone know how I can do this?

An example would be gmail, where you receive the files and when you click on them they open and then you have the option to download


Solution

  • Image Thumb Widget

    ListTile(
      leading: Image.network(imageUrl),
      title: const Text('Image file'),
      subtitle: const Text('Tap for preview'),
      onTap: () => showPreview(imageUrl),
    )
    

    Image Preview Dialog

    void showPreview(String url) {
        showDialog(
            context: context,
            builder: (c) => AlertDialog(
                  title: Row(
                    children: [
                      const Expanded(child: Text('Image File')),
                      IconButton(
                          onPressed: () => download(url),
                          icon: const Icon(Icons.download))
                    ],
                  ),
                  content: Image.network(url),
                ));
      }
    

    Download Code

    void download(String url) {
        http.get(Uri.parse(url)).then((value) {
          if (value.statusCode == 200) {
            Uint8List bytes = value.bodyBytes;
            print('Bytes :: ${bytes.length}');
          }
        });
      }
    

    I used http package for downloads