如何在Flutter Web中调整窗口大小时修复CircleAvatar内的图像。

huangapple go评论64阅读模式
英文:

How to fix the Image inside CircleAvatar when the window is being resized in flutter web

问题

如何在Flutter Web中调整窗口大小时修复CircleAvatar内的图像。
如何在Flutter Web中调整窗口大小时修复CircleAvatar内的图像。

问题是,我希望即使调整窗口大小,图像也保持固定。这是代码:

CircleAvatar(
     radius: 60.0,
     backgroundImage: AssetImage('images/profile.png'),
),

我尝试过使用SizedBoxFittedBoxAlign和居中对齐,但没有什么真正有效的。我在想是否有可能让它固定在一个位置。

英文:

如何在Flutter Web中调整窗口大小时修复CircleAvatar内的图像。
如何在Flutter Web中调整窗口大小时修复CircleAvatar内的图像。

The thing is I want the image to be fixed even when the window is being resized , Here's the code :

CircleAvatar(
     radius: 60.0,
     backgroundImage: const AssetImage('images/profile.png'),
     ),

I tried using SizedBox , FittedBox Align Center and nothing actually worked , I wondering if is it even possible to make it fix in a place

答案1

得分: 1

尝试像这样使用 Align 小部件:

Align(
  alignment: Alignment.topCenter, // 将 CircleAvatar 对齐到顶部中央。
  child: CircleAvatar(
    radius: 50, // 半径为 50
    backgroundImage: NetworkImage(
      'https://cdn.pixabay.com/photo/2015/03/03/20/42/man-657869_1280.jpg'),
  ),
),

阅读此文档

查看这个

图像 1图像2

英文:

Try Align Widget Like This:

  Align(
                  alignment: Alignment.topCenter,//aligns CircleAvatar to Top Center.
                  child: CircleAvatar(
                    radius: 50,//radius is 50
                    backgroundImage: NetworkImage(
                        'https://cdn.pixabay.com/photo/2015/03/03/20/42/man-657869_1280.jpg'),
                  ),
                ),

Read This Documentation
See This

Image 1 And Image2

答案2

得分: 0

根据屏幕宽度使用媒体查询来更改圆形头像的半径,像这样:

CircleAvatar(
    radius: MediaQuery.of(context).size.width * 0.4,
    backgroundImage: const AssetImage('images/profile.png'),
),

将0.4更改为所需的任何值。

英文:

change the radius of circle avatar according to screen width using media query like this.

CircleAvatar(
     radius: MediaQuery.of(context).size.width * 0.4,
     backgroundImage: const AssetImage('images/profile.png'),
     ),

change 0.4 to any value want

答案3

得分: 0

你可以使用 nb_utils 包来创建响应式应用程序。要创建响应式头像,您可以使用 radius: 40.w,这样 40.w 将根据屏幕宽度来自动调整响应式效果。要进行初始化,将 nb_utils 导入到 main.dart 文件中。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initialize();
  runApp(MyApp());
}

支持的平台:

  1. Android
  2. iOS
  3. Windows
  4. Web
  5. Linux
  6. macOS
CircleAvatar(
  radius: 40.w,
  backgroundImage: const AssetImage('images/profile.png'),
)

您可以根据需要更改 40.w 的值。

英文:

you may use use nb_utils package to create responsive apps, To make responsive avatar you can use radius 40.w. so 40.w will be responsive according to width of screen.
initialize nb_utils in main.dart file for initializing.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await initialize();

  runApp(MyApp());
}

Supported Platform

  1. Android
  2. Ios
  3. Windows
  4. Web
  5. Linux
  6. macos

> CircleAvatar(
> radius: 40.w,
> backgroundImage: const AssetImage('images/profile.png'),
> ),

you may change 40.w according to your need.

答案4

得分: 0

我通过移除BackgroundImage并将图像裁剪成圆形而不是默认的矩形(我将其命名为"profile-modified")来解决了问题。我使用了Image.asset作为CircleAvatar的子元素,并使用了fit: BoxFit.cover

修改后的代码如下:

CircleAvatar(
  radius: 60.0,
  child: Image.asset(
    'images/profile-modified.png',
    fit: BoxFit.cover,
  ),
),
英文:

I got it fixed by removing the BackgroundImage and also cropping the image into circular instead of default rectangle (I named it as "profile-modified"). I used Image.assest as a child of CircleAvatar and using fit: BoxFit.cover .

The modified code looks this way :

                CircleAvatar(
                  radius: 60.0,
                  child: Image.asset(
                    'images/profile-modified.png',
                     fit: BoxFit.cover,
                  ),
                ),

</details>



huangapple
  • 本文由 发表于 2023年3月1日 12:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定