如何在弹奏中使用堆栈小部件中的图像?

发布于 2025-01-21 12:15:48 字数 1785 浏览 0 评论 0原文

在堆栈小部件中使用image时,我遇到了一个错误:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
 ...

这是我的代码:

@override
  Widget build(BuildContext context) {

    return Container(
      margin: const EdgeInsets.symmetric(vertical: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(25.0),
                child: Flexible(
                  child: Image.asset(itemData["image"])
                ),
              ),
              const Positioned(
                 ...
              ),
            ],
          ),
        ],
      ),
    );
  }

当然,我在pubspec.yaml中定义了我的图像资产,然后尝试删除flexible widget也在其上方添加层,但是它仍然无法正常工作...

是我删除fixible widget的错误

The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/image2.jpg

When the exception was thrown, this was the stack:
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>

Image provider: AssetImage(bundle: null, name: "assets/images/image2.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#898db(), name:
"assets/images/image2.jpg", scale: 1.0)

这 堆栈小部件,我的代码怎么了? 谢谢您的到来,希望您能有解决方案。干杯

I got an error when using Image inside Stack widget :

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
 ...

this is my code :

@override
  Widget build(BuildContext context) {

    return Container(
      margin: const EdgeInsets.symmetric(vertical: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(25.0),
                child: Flexible(
                  child: Image.asset(itemData["image"])
                ),
              ),
              const Positioned(
                 ...
              ),
            ],
          ),
        ],
      ),
    );
  }

of course, I defined my image assets in pubspec.yaml and I try to remove flexible widget also add Row layer above it, but it's still not working either...

this is my error when I remove flexible widget :

The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/image2.jpg

When the exception was thrown, this was the stack:
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>

Image provider: AssetImage(bundle: null, name: "assets/images/image2.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#898db(), name:
"assets/images/image2.jpg", scale: 1.0)

So, How I can use (multiple) Images inside Stack Widget, and what's wrong with my code?
thank you for coming and I hope you have the solution. cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-01-28 12:15:48

用扩展的小部件包裹堆栈。我尝试了下面的示例,对我有用

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Stack(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(25.0),
                  child: Flexible(
                    child: Image.network(
                      "https://www.writerswrite.co.za/wp-content/uploads/2017/04/how-long-to-read.png",
                    ),
                  ),
                ),
                Positioned(
                  child: Container(
                    height: 20,
                    color: Colors.red,
                    width: 20,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

Wrap the Stack with expanded widget. I tried with the example below and it worked for me

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(vertical: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Stack(
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(25.0),
                  child: Flexible(
                    child: Image.network(
                      "https://www.writerswrite.co.za/wp-content/uploads/2017/04/how-long-to-read.png",
                    ),
                  ),
                ),
                Positioned(
                  child: Container(
                    height: 20,
                    color: Colors.red,
                    width: 20,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
伴我心暖 2025-01-28 12:15:48
You have done everything perfect. but the one mistake was you wraped Image Widget by flexible. thats why flutter throws error. below code will work fine.

just remove flexible widget.

Container(
              margin: const EdgeInsets.symmetric(vertical: 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(25.0),
                        child: Image.network(
                          "https://www.writerswrite.co.za/wp-content/uploads/2017/04/how-long-to-read.png",
                        ),
                      ),
                      Positioned(
                        child: Container(
                          height: 20,
                          color: Colors.red,
                          width: 20,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            );

Flexible Widget used to adjust the available space near the widgets...

因此,RenderObject无法在UI中绘制您在UI中构建的内容,因为您在图像上缠绕着灵活的

to learn more about flexible refer : https://api.flutter.dev/flutter/widgets/Flexible-class.html

above code will working fine...

图像渲染问题:请参阅 https://docs.flutter.dev/development/platform-integration/web-images

You have done everything perfect. but the one mistake was you wraped Image Widget by flexible. thats why flutter throws error. below code will work fine.

just remove flexible widget.

Container(
              margin: const EdgeInsets.symmetric(vertical: 20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Stack(
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(25.0),
                        child: Image.network(
                          "https://www.writerswrite.co.za/wp-content/uploads/2017/04/how-long-to-read.png",
                        ),
                      ),
                      Positioned(
                        child: Container(
                          height: 20,
                          color: Colors.red,
                          width: 20,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            );

Flexible Widget used to adjust the available space near the widgets...

so renderobject not able to paint what you construct in UI because of you wraped flexible over Image

to learn more about flexible refer : https://api.flutter.dev/flutter/widgets/Flexible-class.html

above code will working fine...

for image rendering issue : refer https://docs.flutter.dev/development/platform-integration/web-images

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文