【问题标题】:How to control image size in container?如何控制容器中的图像大小?
【发布时间】:2020-12-25 08:58:41
【问题描述】:

在卡片内有一列,其中包含 2 个灵活小部件(具有 flex 属性 3,1),每个小部件都包含一个容器小部件 这是代码:

@override
  Widget build(BuildContext context) {
    return Container(
          color: Colors.blue,
          child: Card(
            child: Column(
              children: [
                Flexible(
                  flex: 3,
                  child: Container(
                    color: Colors.red,
                  )
                  ),

                Flexible(
                  flex: 1,
                    child: Container(
                      color: Colors.amber,
                      child: Center(child: Text('Request')
                      ),
                    )
                )
              ],
            ),
          ),
    );


这段代码给我下面的屏幕:

现在我想放置图像以覆盖所有红色区域,

 @override
  Widget build(BuildContext context) {
    return Container(
          color: Colors.blue,
          child: Card(
            child: Column(
              children: [
                Flexible(
                  flex: 3,
                   child: Container(
                    color: Colors.red,
                    child: Image.network(
                      'https://placeimg.com/640/480/any', fit: BoxFit.cover,)
                  )
                  ),

                Flexible(
                  flex: 1,
                    child: Container(
                      color: Colors.amber,
                      child: Center(child: Text('Request')
                      ),
                    )
                )
              ],
            ),
          ),
    );
  }

但是当在容器中添加图像时会发生这种情况

我尝试更改适合:BoxFit.cover,但没有任何反应

如何在不改变容器大小的情况下使图像覆盖容器(红色区域)?

添加行和其他列

 Widget build(BuildContext context) {
    return Container(
          color: Colors.blue,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Card(
                child: Column(
                  children: [
                    Flexible(
                      flex: 3,
                      child: Container(
                        // use the decoration property
                        decoration: BoxDecoration(
                          color: Colors.red,
                          // image here
                          image: DecorationImage(
                            image: NetworkImage(
                              'https://placeimg.com/640/480/any',
                            ),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    ),

                    Flexible(
                      flex: 1,
                        child: Container(
                          color: Colors.amber,
                          child: Center(
                              child: RichText(
                                text: TextSpan(
                                  text: 'Request a service',
                                  style: TextStyle(
                                    fontSize: 20,
                                    color: Colors.black
                                  )
                                ),
                          )
                          ),
                        )
                    )
                  ],
                ),
              ),
              Card(
                child: Column(
                  children: [
                    Flexible(
                      flex: 3,
                      child: Container(
                        // use the decoration property
                        decoration: BoxDecoration(
                          color: Colors.red,
                          // image here
                          image: DecorationImage(
                            image: NetworkImage(
                              'https://placeimg.com/640/480/any',
                            ),
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                    ),

                    Flexible(
                        flex: 1,
                        child: Container(
                          color: Colors.amber,
                          child: Center(
                              child: RichText(
                                text: TextSpan(
                                    text: 'Request a service',
                                    style: TextStyle(
                                        fontSize: 20,
                                        color: Colors.black
                                    )
                                ),
                              )
                          ),
                        )
                    )
                  ],
                ),
              ),
            ],
          ),
    );
  }

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    使用Containerdecoration 属性来代替Image

              Flexible(
                    flex: 3,
                    child: Container(
                      // use the decoration property
                      decoration: BoxDecoration(
                        color: Colors.red,
                        // image here
                        image: DecorationImage(
                          image: NetworkImage(
                            'https://placeimg.com/640/480/any',
                          ),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ),
    

    结果:

    编辑:“为什么我的图片在Row 中使用时没有显示?”:

    您需要通过包装SizedBoxContainer 为您的Card 小部件提供Width

    以您的代码为例添加了一个演示:

    class Help extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            color: Colors.blue,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                SizedBox(
                  width: 150, // give width here
                  child: Card(
                    child: Column(
                      children: [
                        Expanded(
                          flex: 3,
                          child: Container(
                            // use the decoration property
                            decoration: BoxDecoration(
                              color: Colors.red,
                              // image here
                              image: DecorationImage(
                                image: NetworkImage(
                                  'https://placeimg.com/640/480/any',
                                ),
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        ),
                        Flexible(
                            flex: 1,
                            child: Container(
                              color: Colors.amber,
                              child: Center(
                                  child: RichText(
                                text: TextSpan(
                                    text: 'Request a service',
                                    style: TextStyle(
                                        fontSize: 20, color: Colors.black)),
                              )),
                            ))
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  width: 150, // give width here
                  child: Card(
                    child: Column(
                      children: [
                        Expanded(
                          flex: 3,
                          child: Container(
                            // use the decoration property
                            decoration: BoxDecoration(
                              color: Colors.red,
                              // image here
                              image: DecorationImage(
                                image: NetworkImage(
                                  'https://placeimg.com/640/480/any',
                                ),
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                        ),
                        Flexible(
                            flex: 1,
                            child: Container(
                              color: Colors.amber,
                              child: Center(
                                  child: RichText(
                                text: TextSpan(
                                    text: 'Request a service',
                                    style: TextStyle(
                                        fontSize: 20, color: Colors.black)),
                              )),
                            ))
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    注意:建议为Card 指定基于设备屏幕的宽度。

    结果:

    【讨论】:

    • 会不会,这解决了问题,但是当我将卡片排成一行时,图像没有显示任何想法? ```行(孩子:[卡(```
    • 您能否更新您的问题,说明您如何将Card 放在Row 中?
    • 如果我的回答对您原帖中指定的问题有帮助,请采纳。如果没有,请提供更多详细信息,说明为什么没有。 @AbrahemGh
    • 我更新了我的答案以解决 image 未显示的问题。看看@AbrahemGh
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多