Flutter如何处理框内固定大小的图像?

2024-01-06

我是 Flutter 的新手,我喜欢它,但我对构建布局感到不舒服。

我正在开发一个包含卡片列表视图的应用程序。 每张卡片都位于容器内,并包含图像(具有固定的高度和宽度)和文本。

我无法将图像正确放置在卡内。我希望图像覆盖盒子的宽度。 谢谢。

这是代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'MyApp';

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: ListView(
          children: <Widget>[
              Container(
                    margin:EdgeInsets.all(8.0),
                    child: Card(
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
                        child: InkWell(
                           onTap: () => print("ciao"),     
                           child: Column(
                                children: <Widget>[
                                    ClipRRect(
                                      borderRadius: BorderRadius.only(
                                        topLeft: Radius.circular(8.0),
                                        topRight: Radius.circular(8.0),
                                      ),
                                      child: Image.asset(
                                        'img/britannia.jpg',
                                        width: 300,
                                        height: 150,
                                        fit:BoxFit.fill  

                                      ),
                                    ),
                                    ListTile(
                                      title: Text('Pub 1'),
                                      subtitle: Text('Location 1'),
                                    ),
                                ],
                           ),
                        ),
                    ),
              ),
          ],
        ),
      ),
    );
  }
} 

您需要添加 -crossAxisAlignment: CrossAxisAlignment.stretch, in Column这样孩子们就可以占据水平空间。

工作代码:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'MyApp';

return MaterialApp(
  title: title,
  home: Scaffold(
    appBar: AppBar(
      title: Text(title),
    ),
    body: ListView(
      children: <Widget>[
        Container(
          margin:EdgeInsets.all(8.0),
          child: Card(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            child: InkWell(
              onTap: () => print("ciao"),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,  // add this
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(8.0),
                      topRight: Radius.circular(8.0),
                    ),
                    child: Image.network(
                        'https://placeimg.com/640/480/any',
                       // width: 300,
                        height: 150,
                        fit:BoxFit.fill

                    ),
                  ),
                  ListTile(
                    title: Text('Pub 1'),
                    subtitle: Text('Location 1'),
                  ),
                ],
              ),
            ),
          ),
        ),
        Container(
          margin:EdgeInsets.all(8.0),
          child: Card(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            child: InkWell(
              onTap: () => print("ciao"),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(8.0),
                      topRight: Radius.circular(8.0),
                    ),
                    child: Image.network(
                        'https://placeimg.com/640/480/any',
                        // width: 300,
                        height: 150,
                        fit:BoxFit.fill

                    ),
                  ),
                  ListTile(
                    title: Text('Pub 1'),
                    subtitle: Text('Location 1'),
                  ),
                ],
              ),
            ),
          ),
        ),
        Container(
          margin:EdgeInsets.all(8.0),
          child: Card(
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
            child: InkWell(
              onTap: () => print("ciao"),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(8.0),
                      topRight: Radius.circular(8.0),
                    ),
                    child: Image.network(
                        'https://placeimg.com/640/480/any',
                        // width: 300,
                        height: 150,
                        fit:BoxFit.fill

                    ),
                  ),
                  ListTile(
                    title: Text('Pub 1'),
                    subtitle: Text('Location 1'),
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    ),
      ),
    );
  }
}

output:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter如何处理框内固定大小的图像? 的相关文章

随机推荐

  • Heroku 架构加载:数据库“postgres”用户的权限被拒绝

    我正在尝试将 Rails 应用程序部署到 Heroku 现在想要加载我的架构 我收到以下错误 Running bundle exec rake db schema load attached to terminal up run 1533
  • 如何告诉 Play Framework 2 和 Ebean 保存空字段?

    我正在使用 Play Framework 2 和 Ebean 当用户提交表单来编辑数据库中的现有对象时 它不会保存空值 我想这是为了防止用 null 覆盖表单中不存在的字段 但是 如果需要的话 我怎样才能让他们将表单中的字段设置为空呢 例如
  • 管道:错误的文件描述符

    我知道以前有人问过这类帖子 但他们的水平显然比我想象的要高 读完他们的帖子后我仍然不明白 所以我决定从这里再次发布这个问题 我正在学习使用管道进行多进程通信 我遇到了这个名为 错误文件描述符 的错误 我不明白为什么我的代码中会出现此错误 i
  • 文本索引 MongoDB,搜索字符串的最小长度

    我已经从 mongo shell 为集合 X 创建了一个文本索引 db X ensureIndex name text cusines text address city text 现在如果一个文档的 name 属性有一个值seasons
  • 响应式设计中的固定位置

    我正在使用响应式框架 1140px 在移动版本中 我必须修复徽标的位置 但不能使其与滚动内容重叠 基本上固定位置但不固定滚动图像 这可以实现吗 你想使用position absolute position fixed将元素固定在屏幕上 因此
  • java中通过HTTP Get下载文件

    我编写了一个下载 Servlet 以根据 messageID 参数返回文件 下面是 doGet 方法 Override protected void doGet HttpServletRequest request HttpServletR
  • 使用 Visual Basic 在 Windows 窗体中嵌入 DOS 控制台

    我设法从其他问题中找到一些数据 使我能够实现下一个代码 Imports System Runtime InteropServices Public Class Form1
  • 错误消息:“无法连接到 Gmail”

    有时我会从许多脚本的基于时间的触发器中收到此错误消息 我可以做些什么还是只是服务器端问题 这看起来确实像服务器端 但预计它偶尔会失败 我想你应该实施一个指数退避 http googleappsdeveloper blogspot com b
  • SQLite 数据库创建时自动增量附近的语法错误

    又是我 那个使用 SQLite net 的人 当我的表主键上没有自动增量时 我的代码可以正常工作 我想自动增量键 所以我像这样重建了表 using SQLite namespace VehicleTracks Models public c
  • Automapper 与 Dapper 的地图绘制对比

    这个问题是为了验证当前的实施在最佳实践和性能方面是否是正确的方法 到目前为止 在我之前的所有公司中 我一直在使用 Auto Mapper 将关系对象映射到域模型实体 并将域模型实体映射到 Dtos ORM 工具是实体框架 在我目前的公司中
  • 观看 OS 2 问题以设置图像

    我只想将图像添加到我的 WKInterfaceController 但是 Xcode 告诉我 在 Watch 上找不到名为 circle44 的图片 IBOutlet var cirlceImage WKInterfaceImage ove
  • JSON 模式:日期大于其他日期

    我有一个像这样的 json 模式 schema http json schema org draft 04 schema title Operation description The schema of an operation type
  • 如何管理故事板中的项目以添加展开/折叠 tableView 单元格的可能性

    我试图在我的 tableView 中创建通过单击展开和折叠各个单元格的能力 不考虑代码 我如何在情节提要中设置元素和约束 从某种意义上说 在我的单元格的内容视图中 我应该添加多少个 UIView 假设我想在展开单元格时将单元格的大小加倍 那
  • 使用 GD 调整图像大小和裁剪图像,同时保留纵横比

    我目前正在编写一个基于Uploadify 的上传器脚本 现在 我调整给定图像和水印的大小之一 这一切都运行良好 但我需要脚本来调整高度大小 然后裁剪宽度 以便长宽比不会混乱 到目前为止 这是我的代码 if fileExtension jpg
  • 列表的排列 - Haskell

    我想用 2 个列表创建所有可能的子组组合 这是一个执行此操作的函数 getCombinations a gt a getCombinations na do a lt na b lt na a b 如果将 abc 传递给此函数 它将返回 a
  • Julia 相当于 R 的 ?? (双问号help.search所有包)

    In R你可以搜索文档 https www r project org help html通过输入问号 或双问号 如何在 Julia REPL 的手册中搜索字符串 gt first No documentation for first in
  • 如何在 Spring Boot 中将多部分文件从一个服务传递到另一个服务?

    我想将多部分文件从一项服务传递到另一项服务 客户端 gt 服务1 gt 服务2 这显示错误 500 内部服务器错误 当前请求不是多部分请求 当我将文件从 Service1 传递到 Service2 时 客户端 gt Service2 当我直
  • Laravel 4 - unserialize():32 字节偏移量 0 处出错

    当我安装 Laravel 4 时 我收到此错误 ErrorException unserialize Error at offset 0 of 32 bytes C xampp htdocs blog laravel bootstrap c
  • Android N:在更新复选框首选项之前检查 DND 状态是否已更改

    我的应用程序将铃声模式设置为静音 但使用 Android N 我得到了java lang SecurityException Not allowed to change Do Not Disturb state 我按照这个步骤操作post
  • Flutter如何处理框内固定大小的图像?

    我是 Flutter 的新手 我喜欢它 但我对构建布局感到不舒服 我正在开发一个包含卡片列表视图的应用程序 每张卡片都位于容器内 并包含图像 具有固定的高度和宽度 和文本 我无法将图像正确放置在卡内 我希望图像覆盖盒子的宽度 谢谢 这是代码