flutter - 图像未覆盖 flutter 中的完整设备尺寸

标签 flutter dart flutter-layout

我正在尝试添加覆盖设备宽度和高度的图像,并且该图像还需要在其上包含文本。首先,在添加图像时,我在 Image.dart 文件上使用了 fit: BoxFit.fill ,以便图像覆盖整个屏幕并且可以正常工作。然后,为了将文本放在图像上,我添加了 Stack 小部件来包裹图像和文本,一旦我这样做,文本就位于图像上,但现在图像没有覆盖整个屏幕高度。为什么会出现这个问题?

// main.dart
void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ImageContainer(),
    );
  }
}

// ImageContainer.dart
class ImageContainer extends StatelessWidget {
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Stack(
            children: [
              MainImage("assets/images/startwars.jpg"),
              Positioned(
                bottom: 0,
                left: 0, 
                child: Container(
                  child: Text(
                    "someText",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.white),
                  )
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}

// Image.dart
class MainImage extends StatelessWidget {
  final String _assetPath;
  MainImage(this._assetPath);
  
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: Colors.red,),
      child: Expanded(
        child: Image.asset(
          _assetPath,
          fit: BoxFit.fill,
        ),
      ),
    );
  }
}

As you can see on this image, the picture is not filling the complete height of the screen

如果您有任何疑问,请在评论中告诉我;)

最佳答案

设置您的Stack适合展开:

Stack(
  fit: StackFit.expand,
  children: [],
);

关于flutter - 图像未覆盖 flutter 中的完整设备尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63431044/

相关文章:

android - 从 tmp 文件夹从二进制存储读取数据的问题

android - 网络图像适合卡片而不会丢失卡片形状, GridView 在 flutter 中

oauth-2.0 - Dart 中的服务器端 OAuth2

flutter - 如何在 z 方向上赋予小部件厚度?

dart - 加载应用程序时,如何禁止 RegistrationPage 短暂出现/闪烁?

当指针到达边缘时 flutter 滚动屏幕

flutter - 如何在特定时间使用 awesome 包在 Flutter 中安排通知?

flutter - 如果语句在我的 Dart Flutter 代码中被注册为变量?我该如何改变?

firebase - 如何在Fire Base中存储图像并在Firestore中存储URL

dart - 如何在 Flutter 中实现滑动到上一页?