Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an image provider from an Icon in flutter for FadeInImage widget?

Tags:

I want to use FadeInImage with an icon as a placeholder.

FadeInImage has few constructors.

In a default constructor it takes ImageProvider as a placeholder, and in FadeInImage.MemoryNetwork( it takes Uint8List as a memory placeholder.

The third constructor creates AssetImage, but I doubt that is useful here.

Is there any way to convert an Icon to one of those data types?

Example signature:

FadeInImage(placeholder: iconPlaceholder, image: NetworkImage("url"))

like image 646
Tree Avatar asked Jun 06 '18 09:06

Tree


People also ask

How do I use icon as image in Flutter?

To set the image to be displayed as the icon, you need to pass an ImageProvider instance. For that purpose, you need to create an instance of any class that's a descendant of ImageProvider such as AssetImage , NetworkImage , MemoryImage , and ResizeImage . The below example uses AssetImage to load the image.

Is icon a widget in Flutter?

A graphical icon widget drawn with a glyph from a font described in an IconData such as material's predefined IconDatas in Icons. Icons are not interactive. For an interactive icon, consider material's IconButton. There must be an ambient Directionality widget when using Icon.


2 Answers

Icons are really just text from a font, so its tricky to make that into an image. The trick in the cookbook should work. Use a Stack with the Icon behind and a transparent placeholder.

body: Stack(
  children: <Widget>[
    Center(child: Icon(Icons.something)),
    Center(
      child: FadeInImage.memoryNetwork(
        placeholder: kTransparentImage,
        image:
            'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true',
      ),
    ),
  ],
),

For kTransparentImage, see this.

like image 97
Richard Heap Avatar answered Sep 17 '22 12:09

Richard Heap


There is also a package that can take Widget as a placeholder

https://pub.dartlang.org/packages/cached_network_image

  ClipOval(
    child: CachedNetworkImage(
  placeholder: new Container(
    height: height,
    width: height,
    child: Icon(Icons.accessibility),
    color: kGrey400,
  ),
  imageUrl: photoUrl,
  height: height,
  fit: BoxFit.cover,
));
like image 28
Tree Avatar answered Sep 16 '22 12:09

Tree