【问题标题】:How to display a circle avatar image in flutter?如何在颤动中显示圆形头像图像?
【发布时间】:2020-08-09 23:40:01
【问题描述】:

使用以下代码时,我希望得到一个圆形头像图像,但得到一个椭圆形。我在 Container 上尝试了不同的参数,例如宽度和高度,但没有帮助。

 appBar: AppBar(
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: new Icon(Icons.star_border, color: Colors.black),
          onPressed: () => {},
        ),
        actions: <Widget>[
          Container(
            //height: 25.0,
            // width: 25.0,
            child: CircleAvatar(
              backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
            )
            /*
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              image: DecorationImage(
                  fit: BoxFit.fill,
                  image: NetworkImage(
                      'https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c')),
            ),*/
          ),
        ],

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您收到的是椭圆形,因为您在高度有限的Appbar 小部件中使用了CircleAvatar 小部件。

    尝试在CircleAvatar Widget 中添加参数radius 它将返回您想要的图像的圆形形状。

    尝试根据需要更改半径大小值。

    CircleAvatar(
              backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
             radius: 15.0
            )
    

    【讨论】:

    • 解决了我的问题,谢谢。我还是不明白为什么容器的height值被忽略了。
    【解决方案2】:

    我过去曾遇到过这个问题,并发现将 AvatarCircle 包装在一个宽度为 58 的容器中可以解决圆形半径比问题,使其成为正确的圆形。或多或少一个像素可能适合您的喜好。试试这个:

    appBar: AppBar(
        backgroundColor: Colors.white,
        leading: IconButton(
          icon: new Icon(Icons.star_border, color: Colors.black),
          onPressed: () => {},
        ),
        actions: <Widget>[
          Container(
            width: 58.0,
            child: CircleAvatar(
              backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
            )
          ),
        ],
    

    【讨论】:

      【解决方案3】:

      我使用此代码并适用于各种图像尺寸...

       CircleAvatar(
              child: ClipOval(
                child: Image.network(items.logo, fit: BoxFit.fill),
              ),
              backgroundColor: Colors.transparent,
              radius: 30,
            )
      

      【讨论】:

        【解决方案4】:

        除了用 padding 的 8.0 包裹 CircleAvatar 之外,没有什么对我有用。

        Padding(
          padding: const EdgeInsets.all(8.0),
          child: CircleAvatar(
            ...
          ),
        )
        

        【讨论】:

          猜你喜欢
          • 2022-08-15
          • 2023-02-03
          • 2022-07-07
          • 2014-06-27
          • 2019-11-08
          • 2021-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多