how to assetimage flutter?

191 views Asked by At

permission requested, here I have some code, which I want to ask, how do I change the shape of the "box" into several images. so when you first click it will change the image and it will fall, after the random response changes to the next image.

(main.dart)

import 'package:flutter/material.dart';
import 'package:flame/flame.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/box-game.dart';
import 'package:flutter/gestures.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  //Util flameUtil = Util();
  await Flame.util.fullScreen();
  await Flame.util.setOrientation(DeviceOrientation.portraitUp);

  BoxGame game = BoxGame();
 runApp(MaterialApp(
    home: Scaffold(
      body: Stack(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTapDown: (TapDownDetails d) {
              // place your tap down handler code here
              //print('outer tap down');
              game.onTapDown(d);
            },

  child: game.widget,
          ),
          Text('This is the HUD UI'),
        ],
      ),
    ),
  ));
}`enter code here`
1

There are 1 answers

0
Farhan Aang On

(box-game.dart)

import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/flame.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/components/fly.dart';
import 'dart:math';
import 'package:flutter/gestures.dart';

class BoxGame extends Game {
  Size screenSize;
  double tileSize;
  // List<Fly> flies;
  List<Fly> flies = <Fly>[];

  Random rnd;

  BoxGame() {
    initialize();
}
void initialize() async {
    // flies = List<Fly>();
    flies = <Fly>[];
    rnd = Random();
    resize(await Flame.util.initialDimensions());

    spawnFly();
  }

  void spawnFly() {
    //flies.add(Fly(this, 50, 50));
    double x = rnd.nextDouble() * (screenSize.width - tileSize);
    double y = rnd.nextDouble() * (screenSize.height - tileSize);
    flies.add(Fly(this, x, y));
  }

  void render(Canvas canvas) {
    // ignore: todo
    // TODO: implement render
    Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
    Paint bgPaint = Paint();
    bgPaint.color = Color(0xfffffffff);
    canvas.drawRect(bgRect, bgPaint);

    flies.forEach((Fly fly) => fly.render(canvas));
}

void onTapDown(TapDownDetails d) {
    print('Box tap down');
    flies.forEach((Fly fly) {
      if (fly.flyRect.contains(d.globalPosition)) {
        fly.onTapDown();
        print('Box Fly outer tap down');
      }
    });
  }

  void update(double t) {
    // ignore: todo
    // TODO: implement update
    flies.forEach((Fly fly) => fly.update(t));
    flies.removeWhere((Fly fly) => fly.isOffScreen);
  }

  void resize(Size size) {
    screenSize = size;
    tileSize = screenSize.width / 9;
  }
}