【问题标题】:Adding background in flutter在颤动中添加背景
【发布时间】:2021-02-12 07:44:08
【问题描述】:

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

这是结果

这是背景

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

【问题讨论】:

    标签: flutter dart flame


    【解决方案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。

    【讨论】:

    • pubspec.yaml 为什么?
    • 确保在assets: 部分下正确设置给定路径。
    • 我在pubspec.yaml 中没有资产部分怎么办?
    • 我更新了答案。请查看最新部分。
    • 我按照您的指示操作,但图像仍然不适用
    猜你喜欢
    • 2019-09-07
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2020-09-27
    • 2023-03-12
    • 2019-12-27
    • 2019-10-22
    相关资源
    最近更新 更多