简体   繁体   English

Flutter CircleAvatar - 使用图像 object 作为(背景)图像/将其类型转换为 ImageProvider

[英]Flutter CircleAvatar - Using Image object as (background) image / typecasting it to ImageProvider

I am trying to show a CircleAvatar at the top of the main screen of my App, but I am running into a rather silly issue.我试图在我的应用程序主屏幕的顶部显示一个CircleAvatar ,但我遇到了一个相当愚蠢的问题。 I am unable to figure out how to provide an object of the class Image (that is a property of a user) as an ImageProvider , which is what CircleAvatar expects .我无法弄清楚如何将 class Image (这是用户的属性)的 object 提供为ImageProvider ,这是CircleAvatar 期望的。 Is there an easy way to "typecast" here or some other way people have resolved this?有没有一种简单的方法可以在这里“打字”或人们解决这个问题的其他方式?

class _HomePageState extends State<PictureShowcaseHome>{
  Appuser appuser = new Appuser();
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: InkWell(          
          child: CircleAvatar(                               
                  backgroundImage: appuser.img, 
                  child: Text(appuser.name.substring(0,1).toUpperCase()),
                ) ,
        ),

...

To simplify things, assume an Appuser has just the following properties:为简化起见,假设Appuser仅具有以下属性:

class Appuser{
  String name;
  Image img;  
  Appuser(){
    this.name = "Unknown user";
    this.img = Image.asset('assets/imgs/default1.jpg');        
  }
}

My idea is that I have a default image that is included in the assets, but the user can replace it with their own image (seems like a rather standard idea).我的想法是我有一个包含在资产中的默认图像,但用户可以用他们自己的图像替换它(似乎是一个相当标准的想法)。 I considered switching to AssetImage img;我考虑切换到AssetImage img; and img = new AssetImage('assets/imgs/default1.jpg');img = new AssetImage('assets/imgs/default1.jpg'); in the constructor for Appuser , but that sort of seems backwards.Appuser的构造函数中,但那种似乎倒退了。 It also gets me into the "opposite" problem, when I display the user image on a user information details page (I thought of just having a ListTile( title: appuser.img,), ) or when I let the user take a new user picture with the camera.当我在用户信息详细信息页面上显示用户图像时(我想只有一个ListTile( title: appuser.img,), )或者当我让用户使用新的用户使用相机拍照。 For those purposes, it is easiest to directly work with an object of class Image , not an AssetImage .出于这些目的,最简单的方法是直接使用 class Image的 object ,而不是AssetImage

Or am I somehow looking at this the wrong way?还是我以某种方式看错了?

This这个

Image.asset('assets/imgs/default1.jpg').image 

should work.应该管用。

For the sake of clarity, the Image class has an ImageProvider property.为了清楚起见,Image class 具有 ImageProvider 属性。 Just get the ImageProvider using.image with any given Image class.只需使用任何给定的图像 class 获取 ImageProvider using.image。 This works for Image.asset, .file, .network, and.memory这适用于 Image.asset、.file、.network 和.memory

.image will give you the ImageProvider that is needed. .image将为您提供所需的 ImageProvider。

CircleAvatar(
        backgroundColor: Colors.red,
        radius: radius,
        child: CircleAvatar(
            radius: radius - 5,
            backgroundImage: Image.file(
              profileImage,
              fit: BoxFit.cover,
            ).image,
        ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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