fluttercommunity / flutter_blurhash

Compact representation of a placeholder for an image. Encode a blurry image under 30 caracters for instant display like used by Medium. Maintainer: @Solido
https://pub.dev/packages/flutter_blurhash
MIT License
513 stars 65 forks source link

Images Flickers while loading #31

Closed SushilGhorasaini1 closed 2 years ago

SushilGhorasaini1 commented 2 years ago

While Using BlurHash as placee holder in Cached Network image, it flickers while loading the image for the first time.

bounty1342 commented 2 years ago

Did you solve it ?

SushilGhorasaini1 commented 2 years ago

yes, I just used blurhash_dart with cache_network_image. Here is what i did.

import 'dart:typed_data';

import 'package:blurhash_dart/blurhash_dart.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:image/image.dart' as img;

class ImageWidget extends StatefulWidget { final String imageUrl; final String hash; final BoxFit fit; final Widget Function(BuildContext, ImageProvider)? imageBuilder; final double? height; final double? width; final FilterQuality filterQuality; const ImageWidget({ Key? key, required this.imageUrl, required this.hash, this.fit = BoxFit.cover, this.imageBuilder, this.height, this.width, this.filterQuality = FilterQuality.medium, }) : super(key: key);

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

class _ImageWidgetState extends State { late Uint8List bytes; @override void initState() { final BlurHash blurHash = BlurHash.decode(widget.hash); final img.Image _image = blurHash.toImage(35, 20); bytes = Uint8List.fromList(img.encodeJpg(_image)); super.initState(); }

@override Widget build(BuildContext context) { return CachedNetworkImage( height: widget.height, width: widget.width, imageUrl: widget.imageUrl, imageBuilder: widget.imageBuilder, fit: widget.fit, filterQuality: widget.filterQuality, placeholder: (context, url) => LayoutBuilder( builder: (context, constraints) { final widgetHeight = constraints.maxHeight; return SizedBox( height: widgetHeight, child: Image.memory( bytes, fit: BoxFit.cover, ), ); }, ), ); } }

amanokerim commented 2 years ago

Structured version of @SushilGhorasaini1 code:

import 'dart:typed_data';

import 'package:blurhash_dart/blurhash_dart.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;

class ImageWidget extends StatefulWidget {
  final String imageUrl;
  final String hash;
  final BoxFit fit;
  final Widget Function(BuildContext, ImageProvider)? imageBuilder;
  final double? height;
  final double? width;
  final FilterQuality filterQuality;
  const ImageWidget({
    Key? key,
    required this.imageUrl,
    required this.hash,
    this.fit = BoxFit.cover,
    this.imageBuilder,
    this.height,
    this.width,
    this.filterQuality = FilterQuality.medium,
  }) : super(key: key);

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

class _ImageWidgetState extends State {
  late Uint8List bytes;
  @override
  void initState() {
    final BlurHash blurHash = BlurHash.decode(widget.hash);
    final img.Image _image = blurHash.toImage(35, 20);
    bytes = Uint8List.fromList(img.encodeJpg(_image));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      height: widget.height,
      width: widget.width,
      imageUrl: widget.imageUrl,
      imageBuilder: widget.imageBuilder,
      fit: widget.fit,
      filterQuality: widget.filterQuality,
      placeholder: (context, url) => LayoutBuilder(
        builder: (context, constraints) {
          final widgetHeight = constraints.maxHeight;
          return SizedBox(
            height: widgetHeight,
            child: Image.memory(
              bytes,
              fit: BoxFit.cover,
            ),
          );
        },
      ),
    );
  }
}