在颤振中添加背景

时间:2021-02-12 07:44:08

标签: flutter dart flame

我应该怎么做才能在我的项目中应用我的背景图片?我错过了什么?我在 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;
  }
}

这是结果

enter image description here

这是背景

enter image description here

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

1 个答案:

答案 0 :(得分: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

阅读 official document 中的更多内容。