4

can you suggest a way to zoom an image inside a CachedNetworkImage?

Here is my code

CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
     ),

I tried to wrap CachedNetworkImage in a photo_view widget but it does not work

@override
Widget build(BuildContext context) {
  return Container(
    child: PhotoView(
      imageProvider: CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        placeholder: (context, url) => CircularProgressIndicator(),
        errorWidget: (context, url, error) => Icon(Icons.error),
      )
    )
  );
}
0

3 Answers 3

12

You can copy paste run full code below
Package Cached network image provide CachedNetworkImageProvider
code snippet

PhotoView(
    imageProvider:
        CachedNetworkImageProvider("http://via.placeholder.com/350x150"),
  )

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:cached_network_image/cached_network_image.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(flex: 1, child: PhotoViewTest()),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class PhotoViewTest extends StatefulWidget {
  @override
  _PhotoViewTestState createState() => _PhotoViewTestState();
}

class _PhotoViewTestState extends State<PhotoViewTest> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: PhotoView(
        imageProvider:
            CachedNetworkImageProvider("http://via.placeholder.com/350x150"),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

is there a simple way to still include a placeholder and an errorWidget as per CachedNetworkImage? CachedNetworkImageProvider only accept the url
I have checked source code. CachedNetworkImageProvider only accept the url currently.
10

You can wrap Photo view inside Cached Network image like this code, so you can use advantages of both cached network image and photo view

CachedNetworkImage(
    imageUrl: "http://via.placeholder.com/350x150",
    imageBuilder: (context, imageProvider) => PhotoView(
        imageProvider: imageProvider,
    ),
    placeholder: (context, url) =>
        CircularProgressIndicator(),
        errorWidget: (context, url, error) =>
            Icon(Icons.error),
)

2 Comments

While it’s acceptable to provide code-only answers, it’s often more useful for the community if you can also provide an explanation of the code and help people understand why it addresses the problem. That can reduce the number of follow-up questions, and help new developers understand the underlying concepts. Would you mind updating your question with additional detail?
for some reason the fit property with BoxFit.cover doesn't work in this scenario
1
Widget build(BuildContext context) { 
  return Container(
     child: CachedNetworkImage(
        imageUrl: "http://via.placeholder.com/350x150",
        imageBuilder: (context, imageProvider) => PhotoView(
           imageProvider: imageProvider,
        )
     ),
  );
}

1 Comment

@yellowgray sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.