0

我应该怎么做才能在我的项目中应用我的背景图片?我错过了什么?我在 lib/assets/background.jpg 下找到的背景图片

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Util flameUtil = Util();
  await flameUtil.fullScreen();
  await flameUtil.setOrientation(DeviceOrientation.portraitUp);

  LangawGame game = LangawGame();
  runApp(game.widget);
}

langaw-game.dart

class LangawGame extends Game {
  Size screenSize;
  double tileSize;
  @override
  void render(Canvas canvas) {
    body:
    Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/background.jpg"),
          fit: BoxFit.cover,
        ),
      ),

    );
  }

  @override
  void update(double t) {}
  @override
  void resize(Size size) {
    super.resize(size);
    screenSize = size;
    tileSize = screenSize.width / 9;
  }
}

这是结果

在此处输入图像描述

这是背景

在此处输入图像描述

我没有收到错误,但图像没有反映

4

1 回答 1

1

您的图像路径不正确。现在您的图像在lib/assets文件夹下,但您正在尝试访问assets/background.jpg. 您需要使用完整路径进行编辑,如下所示:

AssetImage("lib/assets/background.jpg"),

注意:另外,请检查您的pubspec.yaml文件。

flutter:
  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add Flutter specific assets to your application, add an assets section,
  # like this:
  assets:
    - lib/assets/background.jpg

官方文档中阅读更多内容。

于 2021-02-12T07:49:42.400 回答