image_fade 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

ImageFade #

Requires Flutter 1.6.7 or higher.

A widget for Flutter that displays a placeholder widget while a specified image loads, then cross-fades to the loaded image. Also handles progress and errors.

If image is changed, it will cross-fade to the new image once it is finished loading. Setting image to null will cross-fade back to the placeholder.

example image

You can set fadeDuration and fadeCurve, as well as most Image properties: width, height, fit, alignment, repeat, matchTextDirection, excludeFromSemantics and semanticLabel.

You can also specify a loadingBuilder that will display load progress any time a new image is loaded, and an errorBuilder that will display if an error occurs while loading an image.

Example #

See the example directory for a simple example.

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[0.2.1] - 2019-09-02 #

Changed #

  • Now using FadeTransition instead of Opacity for better performance.

[0.2.0] - 2019-07-29 #

Added #

  • Support for error handling via errorBuilder

Changed #

  • placeholder now accepts a Widget instead of an ImageProvider.
  • previously loaded images are now faded out after the new image is faded in. Noticeable when a smaller image is loaded over a larger one.

Removed #

  • backgroundColor was removed. Use a placeholder with a color instead.

[0.1.0] - 2019-07-23 #

Added #

  • First release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:image_fade/image_fade.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.yellow,
      ),
      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> {
  // Sample images from Wikimedia Commons:
  static const List<String> _imgs = [
    'https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Almeida_Júnior_-_Saudade_%28Longing%29_-_Google_Art_Project.jpg/513px-Almeida_Júnior_-_Saudade_%28Longing%29_-_Google_Art_Project.jpg',
    'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Rafael_-_Retrato_de_um_Cardeal.jpg/786px-Rafael_-_Retrato_de_um_Cardeal.jpg',
    'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f5/James_McNeill_Whistler_-_La_Princesse_du_pays_de_la_porcelaine_-_brighter.jpg/580px-James_McNeill_Whistler_-_La_Princesse_du_pays_de_la_porcelaine_-_brighter.jpg',
    'https://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Hans_Holbein_der_Jüngere_-_Der_Kaufmann_Georg_Gisze_-_Google_Art_Project.jpg/897px-Hans_Holbein_der_Jüngere_-_Der_Kaufmann_Georg_Gisze_-_Google_Art_Project.jpg',
    'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Pieter_Bruegel_the_Elder_-_The_Tower_of_Babel_%28Vienna%29_-_Google_Art_Project_-_edited.jpg/1280px-Pieter_Bruegel_the_Elder_-_The_Tower_of_Babel_%28Vienna%29_-_Google_Art_Project_-_edited.jpg',
  ];

  int _counter = 0;
  bool _clear = true;
  bool _error = false;

  void _incrementCounter() {
    setState(() {
      if (_clear || _error) { _clear = _error = false; }
      else { _counter = (_counter+1)%_imgs.length; }
    });
  }

  void _clearImage() {
    setState(() {
      _clear = true;
      _error = false;
    });
  }

  void _testError() {
    setState(() {
      _error = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    ImageProvider image;
    if (_error) { image = NetworkImage('error.jpg'); }
    else if (!_clear) { image = NetworkImage(_imgs[_counter]); }

    return Scaffold(
      appBar: AppBar(
        title: Text('Showing ' + (_error ? 'error' : _clear ? 'placeholder' : 'image #$_counter from Wikimedia')),
      ),

      body: Stack(children: <Widget>[
        Positioned.fill(child: 
          ImageFade(
            image: image,
            placeholder: Container(
              color: Color(0xFFCFCDCA),
              child: Center(child: Icon(Icons.photo, color: Colors.white30, size: 128.0,)),
            ),
            alignment: Alignment.center,
            fit: BoxFit.cover,
            loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent event) {
              if (event == null) { return child; }
              return Center(
                child: CircularProgressIndicator(
                  value: event.expectedTotalBytes == null ? 0.0 : event.cumulativeBytesLoaded / event.expectedTotalBytes
                ),
              );
            },
            errorBuilder: (BuildContext context, Widget child, dynamic exception) {
              return Container(
                color: Color(0xFF6F6D6A),
                child: Center(child: Icon(Icons.warning, color: Colors.black26, size: 128.0)),
              );
            },
          )
        )
      ]),

      floatingActionButton: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
        FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Next',
          child: Icon(Icons.navigate_next),
        ),
        SizedBox(width:10.0),
        FloatingActionButton(
          onPressed: _clearImage,
          tooltip: 'Clear',
          child: Icon(Icons.clear),
        ),
        SizedBox(width:10.0),
        FloatingActionButton(
          onPressed: _testError,
          tooltip: 'Error',
          child: Icon(Icons.warning),
        ),
      ]),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  image_fade: ^0.2.1

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:image_fade/image_fade.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • image_fade that is a package requiring null.

Health suggestions

Format lib/image_fade.dart.

Run flutter format to format lib/image_fade.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test