简体   繁体   English

Flutter:如何在应用栏中制作圆形头像

[英]Flutter: How to make circular avatar in appbar

Good day, I am struggling to make my avatar in the top right of the appbar a perfect circle.美好的一天,我正在努力使应用栏右上角的头像成为一个完美的圆圈。 It keeps coming out oval.它不断出现椭圆形。

在此处输入图片说明

I've tried many options (including setting the radius, using ClipRRect, ClipOval, etc) and it doesn't seem to affect the shape of the rounded edges.我尝试了很多选项(包括设置半径、使用 ClipRRect、ClipOval 等),但它似乎不会影响圆角边缘的形状。

Code:代码:

return Scaffold(
  appBar: AppBar(
    automaticallyImplyLeading: false,
      backgroundColor: COLORS_BG,
      title: Padding(
        padding: const EdgeInsets.only(top: 10.0),
        child: Row(
          children: <Widget>[
            Image.asset('images/localhourlogo.png', height: 50.0,),
          ]
        ),
      ),
      actions: <Widget>[
        PopupMenuButton<String>(
          icon: CircleAvatar(
            backgroundImage: NetworkImage(googleUserUrl)
          ),
          onSelected: choiceAction,
          itemBuilder: (BuildContext context) {
            return MenuItems.choices.map((String choice) {
              return PopupMenuItem<String> (
                value: choice,
                child: Text(choice),
              );
            }).toList();
          },
        )
      ],

Goal: to make the avatar a pure circle and not this oval shape.目标:使头像成为纯圆形而不是椭圆形。

Tried: ClipRRect, ClipOval, setting radius min and max, etc尝试过: ClipRRect、ClipOval、设置半径最小值和最大值等

Thank you for any and all help.感谢您的任何帮助。

I've had this issue in the past and have found that wrapping the AvatarCircle in a container with 58 width fixes the Circle radius ratio issue, making it a proper circle shape.我过去曾遇到过这个问题,并且发现将 AvatarCircle 包裹在一个宽度为 58 的容器中可以修复圆半径比问题,使其成为一个合适的圆形。 One pixel more or less may fit your liking.或多或少一个像素可能符合您的喜好。 Give this a try:试试这个:

Container(
  width: 58,
  child: PopupMenuButton(
    icon: CircleAvatar(
      backgroundImage: NetworkImage(
        "https://4.bp.blogspot.com/-Jx21kNqFSTU/UXemtqPhZCI/AAAAAAAAh74/BMGSzpU6F48/s1600/funny-cat-pictures-047-001.jpg"
      ),
      backgroundColor: Colors.red,
    ),
    itemBuilder: (BuildContext context) {
      return [
        PopupMenuItem<String> (
          value: '1',
          child: Text('1'),
        ),
        PopupMenuItem<String> (
          value: '2',
          child: Text('2'),
        ),
      ];
    },
  ),
)

Normally it should work with ClipRRect .通常它应该与ClipRRect一起ClipRRect Make sure you add fit: BoxFit.cover to avoid scaling.确保添加fit: BoxFit.cover以避免缩放。

ClipRRect(
  borderRadius: BorderRadius.circular(25.0),
  child: Image.network(
    googleUserUrl,
    height: 50.0,
    width: 50.0,
    fit: BoxFit.cover,
  ),
),

This was the solution I found, but now my issue is that I can't make the avatar bigger than what is shown below.这是我找到的解决方案,但现在我的问题是我不能让头像比下面显示的大。

actions: <Widget>[
                  PopupMenuButton<String>(
                    icon: Container(
                      child: ClipOval(
                        child: Align(
                          heightFactor: 1,
                          widthFactor: 1,
                          child: Image.network(googleUserUrl),
                        ),
                      )
                    ),

在此处输入图片说明


With João Soares code:使用 João Soares 代码:

Container(
  padding: EdgeInsets.all(5),
  width: 58,
  child: CircleAvatar(
    backgroundImage: NetworkImage(
      "https://4.bp.blogspot.com/-Jx21kNqFSTU/UXemtqPhZCI/AAAAAAAAh74/BMGSzpU6F48/s1600/funny-cat-pictures-047-001.jpg"
    )
  ),
),

在此处输入图片说明


With dumazy code:使用笨拙的代码:

actions: <Widget>[
                  PopupMenuButton<String>(
                    icon: Container(
                      child: ClipOval(
                        child: Align(
                          heightFactor: 1,
                          widthFactor: 1,
                          child: Image.network(googleUserUrl),
                        ),
                      )
                    ),

在此处输入图片说明


Thoughts?想法?

Here is my final solution thanks in big part to João Soares.这是我的最终解决方案,这在很大程度上要归功于 João Soares。 Adjusting the width increases the size of the circle.调整宽度会增加圆圈的大小。 I made it equal to the height of the "localhour" image, and now it looks great.我使它等于“localhour”图像的高度,现在看起来很棒。

actions: <Widget>[
                  Container(
                    width: 60.0,
                    child: PopupMenuButton<String>(
                      icon: ClipOval(
                        child: Align(
                          heightFactor: 1,
                          widthFactor: 1,
                          child: Image.network(googleUserUrl),
                        ),
                      ),
                      onSelected: choiceAction,
                      itemBuilder: (BuildContext context) {
                        return MenuItems.choices.map((String choice) {
                          return PopupMenuItem<String> (
                            value: choice,
                            child: Text(choice),
                          );
                        }).toList();
                      },
                    ),
                  )
                ],

在此处输入图片说明

You can use ClipOval Widget to do that.您可以使用 ClipOval 小部件来做到这一点。 I am posting an example where my image will be leading icon widget in rounded format just like in native android.我发布了一个示例,其中我的图像将像在原生 android 中一样以四舍五入格式作为领先的图标小部件。

AppBar(
      title: Text(title),
      leading: Padding(
        padding: const EdgeInsets.all(8.0),
        child: ClipOval(
          child: Image(image: Image.asset('assets/images/icon.webp').image,),
        ),
      ),
    );

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

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