flutter - 具有 fitHeight 的 FittedBox 渲染图像宽度的其余部分

标签 flutter dart

在下面的示例中,FittedBox 执行了我想要的操作:它将高度适合容器。但是,它应该隐藏图像的其余部分。但是,它会呈现它。

出了什么问题?

enter image description here

/// Flutter code sample for Expanded

// This example shows how to use an [Expanded] widget in a [Column] so that
// its middle child, a [Container] here, expands to fill the space.
//
// ![This results in two thin blue boxes with a larger amber box in between.](https://flutter.github.io/assets-for-api-docs/assets/widgets/expanded_column.png)

import 'package:flutter/material.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Expanded Column Sample'),
      ),
      body: Center(
        child: Container(
          width: 100,
          child: Column(
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: 100,
              width: 100,
            ),
            Expanded(
              child: Container(
                color: Colors.amber,
                child: FittedBox(
                  fit: BoxFit.fitHeight,
                  child: Image.network(
                    'https://i.ytimg.com/vi/E5fD0SSqPa0/maxresdefault.jpg'))
              ),
            ),
            Container(
              color: Colors.blue,
              height: 100,
              width: 100,
            ),
          ],
        )),
      ),
    );
  }
}

根据https://api.flutter.dev/flutter/widgets/FittedBox-class.html ,它应该适合高度但忽略图像的其余部分

最佳答案

您可以删除 FittedBox 并将 fit 属性直接设置为图像的 BoxFit.cover 值。

return Scaffold(
      appBar: AppBar(
        title: const Text('Expanded Column Sample'),
      ),
      body: Center(
        child: Container(
            width: 100,
            child: Column(
              children: <Widget>[
                Container(
                  color: Colors.blue,
                  height: 100,
                  width: 100,
                ),
                Expanded(
                  child: Container(
                      color: Colors.amber,
                      child: Image.network(
                        'https://i.ytimg.com/vi/E5fD0SSqPa0/maxresdefault.jpg',
                        fit: BoxFit.cover,
                      )),
                ),
                Container(
                  color: Colors.blue,
                  height: 100,
                  width: 100,
                ),
              ],
            )),
      ),
    );

关于flutter - 具有 fitHeight 的 FittedBox 渲染图像宽度的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66629765/

相关文章:

具有相同 key.jks 的 Flutter 签名应用程序无法正常工作

dart - 导入 'package:flutter/foundation.dart' ;没有包装文件

android - 在多个模拟器上运行 flutter 应用程序

firebase - Flutter 细节搜索功能

android-studio - 在Flutter的警报对话框中添加下拉菜单

firebase - Flutter Firebase如何使用Google登录用户

android - Flutter HTTP 服务器 - 在另一个应用程序中提供 Flutter Web 服务

android - 构建失败,但 flutter 朔迷离

firebase - 如何获取在Firebase中具有重复值的字段的长度(如类别)

android - 错误 : unable to locate asset entry in pubspec. yaml: "assets/fonts/Lato-Regular.ttf"