考虑提前调整资产大小,提供 cacheWidth 参数和 cacheHeight 参数

Consider resizing the asset ahead of time, supplying a cacheWidth parameter and cacheHeight parameter

我是 flutter 的新手,我正在使用 flutter web。 并尝试对图像应用背景滤镜。 这是我的代码:

import 'dart:ui';

import 'package:flutter/material.dart';

class WebsiteBackground extends StatelessWidget {
  const WebsiteBackground({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      decoration: const BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.fill,
          image: AssetImage('assets/images/office1.jpeg'),
        ),
      ),
      child: BackdropFilter(
        filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
        child: Container(
          decoration: BoxDecoration(color: Colors.white.withOpacity(0.0)),
        ),
      ),
    );
  }
}

它一直为我吐出这个错误

 ======== Exception caught by painting library ======================================================
The following message was thrown while painting an image:
Image assets/images/office1.jpeg has a display size of 3024×1732 but a decode size of 6000×4000, which uses an additional 97721KB.

Consider resizing the asset ahead of time, supplying a cacheWidth parameter of 3024, a cacheHeight parameter of 1732, or using a ResizeImage.

====================================================================================================
[{"id":655,"result":{"value":"macOS","type":"Response","method":"ext.flutter.platformOverride"}}]

任何帮助将不胜感激,谢谢!

编辑:感谢 @Ruchit' 的评论,通过更改以下内容进行修复

image: AssetImage('assets/images/office1.jpeg'),

image: ResizeImage(AssetImage('assets/images/office1.jpeg'),
              width: 1000, height: 1000),
        ),

这里我假设正在发生的事情是你有一张尺寸为 3024×1732 的图像,但你正在渲染尺寸为 6000×4000 的图像,这对 flutter 来说是一项繁重的工作。所以 flutter 建议您尽早指定图像尺寸,以便它预渲染您的图像。

为此,您使用 ResizeImage() class、https://api.flutter.dev/flutter/painting/ResizeImage-class.html