skip to Main Content

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

2

Answers


  1. Welcome to StackOverflow.

    For displaying a image from a known URL you just need to use Image.network()

    Here is a simple example:

     Image.network(
                  'https://images.unsplash.com/photo-1501927023255-9063be98970c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1950&q=80',
                  fit: BoxFit.contain,
                ),
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search