简体   繁体   中英

How to Use Blurhash in flutter App or Flutter Project

CachedNetworkImage(
   imageUrl: "http://via.placeholder.com/350x150",
   placeholder: (context, url) => new CircularProgressIndicator(),
// i want to use Blurhash on placeholder 
   errorWidget: (context, url, error) => new Icon(Icons.error),
 ),

https://pub.dev/packages/blurhash

i didn't found Any single Tutorial on it.. I Need Help

You can copy paste run full code below
You can await BlurHash image in main() and then use in CachedNetworkImage
code snippet

Uint8List imageDataBytes;
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  imageDataBytes =
      await BlurHash.decode("LBAdAqof00WCqZj[PDay0.WB}pof", 32, 32);
  runApp(MyApp());
}
...
 CachedNetworkImage(
              imageUrl: 'https://via.placeholder.com/150x150',
              placeholder: (BuildContext context, String url) =>
                  Image.memory(imageDataBytes, width: 256, fit: BoxFit.cover),
              errorWidget: (context, url, error) => new Icon(Icons.error),
            ),

working demo snapshot

在此处输入图片说明

full code

import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'dart:typed_data';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:blurhash/blurhash.dart';

Uint8List imageDataBytes;
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  imageDataBytes =
      await BlurHash.decode("LBAdAqof00WCqZj[PDay0.WB}pof", 32, 32);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @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
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CachedNetworkImage(
              imageUrl: 'https://via.placeholder.com/150x150',
              placeholder: (BuildContext context, String url) =>
                  Image.memory(imageDataBytes, width: 256, fit: BoxFit.cover),
              errorWidget: (context, url, error) => new Icon(Icons.error),
            ),
            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),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM