ok_image 0.4.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

OKImage #

Easy to use this library to build a network image widget, you can build different layouts according to the error/load, also set up the image cache delegate.

ok_image pub package GitHub GitHub stars

use #

  1. add to your pubspec.yaml
ok_image: ^0.4.0
  1. import
import "package:ok_image/ok_image.dart";
  1. use
import "package:ok_image/ok_image.dart";
  return OKImage(
      url: "https://ws1.sinaimg.cn/large/844036b9ly1fxfo76hzd4j20zk0nc48i.jpg",
      width: 200,
      height: 200,
      timeout: Duration(seconds: 20),
      fit: fit,
  1. params
url: image net url
width: width
height: height
fit: show BoxFit
followRedirects: whether image redirection is allowed.
loadingWidget: display on loading
errorWidget: display when image load error / timeout.
retry: retry to load image count.
timeout: timeout duration.
onErrorTap: when loadErrorWidget show ,onTap it.
cacheDelegate: you can use the param to delegate loadImage

Experimental: Signatures, return values, parameters and other information may be modified in the future.

onLoadStateChanged: will be call on the load state changed.
  1. global config

edit OKImage.buildErrorWidget to config global OKImage errorWidget.

edit OKImage.buildLoadingWidget to config global OKImage loading.

about other library #

under BSD 3:

  1. This library uses http 0.12.0+2 as a framework for network access.
  2. using path_provider 1.2.0 to get default catch path.
  3. using crypto 2.0.6 to make and check md5.

Apache 2.0:

  1. Using rxdart 0.22.1 processing logic

thanks to open source.

If you are using older versions of these open source libraries, which cause incompatibility, please update your. If it is incompatible with me, please contact me and I will update the version number when appropriate.


[0.4.0] #

Breaking change: Params errorWidget change type to ErrorWidgetBuilder.


  • Download error bug.

[0.3.1] upgrade dependencies version #

  • path_provider
  • rx_dart
  • http

[0.3.0] upgrade path_provider #

breaking change because path_provider migrate from android support to androidX, so your other plugin also need be migrated. upgrade rxdart

[0.2.3] add remove cache #

ImageCache.removeCache(String url);

[0.2.2] request #

add a parameter to delete the cache whose last access time exceeds duration

add onImageLoadState callback

[0.2.1] un-ext image load error #

Fix: Unextended images cannot be loaded

[0.2.0] #

global error and loading widget

add a clear cache method

upgrade rxdart version

fix bug for downloading

[0.1.1] fix load bug #

fix error bug

[0.1.0] update request and cache #

Now the same URL will share a download, waiting for the download to complete and return together.

+1 #

update readme

[0.0.3] add cache delegate #

add a default cache delegate to cache image

[0.0.2] ADD LICENSE #

use MIT

[0.0.1] first version #


import 'package:flutter/material.dart';
import 'package:ok_image/ok_image.dart';
import 'package:ok_image/log.dart' as OL;

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      home: MyHomePage(title: 'Flutter Demo Home Page'),

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  _MyHomePageState createState() => _MyHomePageState();

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

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.

  void initState() {
    OL.Log.showLog = true;
    OKImage.buildErrorWidget = () => Center(
          child: Text("I'm error"),
    OKImage.buildLoadingWidget = () => Row(
          children: [

  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
        actions: <Widget>[
            icon: Icon(Icons.clear),
            onPressed: () => ImageCacheManager().clearAllCache(),
            tooltip: "clear image cache file",
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
              'You have pushed the button this many times:',
              style: Theme.of(context).textTheme.display1,
            buildNetImage(fit: BoxFit.contain),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.

  Widget buildNetImage({BoxFit fit}) {
    return OKImage(
      // url: "https://ws1.sinaimg.cn/large/844036b9ly1fxfo76hzd4j20zk0nc48i.jpg",
      url: "http://avator.eastmoney.com/qface/3241045360517340/166",
//      loadingWidget: CircularProgressIndicator(),
      width: 200,
      height: 200,
      timeout: Duration(seconds: 20),
      fit: fit,
      onLoadStateChanged: _onLoadStateChange,

  void _onLoadStateChange(OnLoadState value) {
    var now = DateTime.now();
    print("$now : on load image state change : $value");

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  ok_image: ^0.4.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:ok_image/ok_image.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (rxdart).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
crypto ^2.0.6 2.1.4
flutter 0.0.0
http ^0.12.0+2 0.12.0+4
path_provider ^1.2.0 1.6.5
rxdart ^0.22.1 0.22.6 0.23.1
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
http_parser 3.1.4
meta 1.1.8
path 1.6.4
path_provider_macos 0.0.4
path_provider_platform_interface 1.0.1
pedantic 1.9.0
platform 2.2.1
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies