flim 1.1.0 copy "flim: ^1.1.0" to clipboard
flim: ^1.1.0 copied to clipboard

minimal data-driven sprite render and animation lib for Flutter

example/lib/main.dart

import 'package:example/my_benchmark_game.dart';
import 'package:flutter/material.dart';

import 'package:flim/flim.dart';
import 'package:asset_cache/asset_cache.dart';

import 'my_game_widget.dart';
import 'my_playground_game.dart';
import 'my_simple_game.dart';
import 'my_keyboard_game.dart';

final imageAssetCache = ImageAssetCache(basePath: "assets/images/");
final jsonAssetCache = JsonAssetCache(basePath: "assets/json/");

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
        appBarTheme: AppBarTheme(
          elevation: 0,
          color: Colors.black26,
        ),
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('flim example'),
      ),
      body: buildGameAndWidgets(),
    );
  }

  Widget buildBenchmarkGame() {
    return FutureBuilder<Game>(
      future: MyBenchmarkGame(MediaQuery.of(context).size)
          .initialize(imageAssetCache),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return GameWidget(snapshot.data!);
        } else {
          return Container();
        }
      },
    );
  }

  Widget buildKeyboardGame() {
    return Stack(
      children: [
        FutureBuilder<MyKeyboardGame>(
          future: MyKeyboardGame(MediaQuery.of(context).size)
              .initialize(imageAssetCache),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return MyGameWidget(snapshot.data!);
            } else {
              return Container();
            }
          },
        ),
        Padding(
          padding: EdgeInsets.all(32),
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Text(
              'press \'hjkl\' to move and \'f\' to fire',
              style: TextStyle(
                fontSize: 17,
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget buildAnimationWithTwoImage() {
    return FutureBuilder<AnimatedSprite>(
      future: AnimatedSprite.loadJson(
        'animation_two_images.json',
        jsonAssetCache,
        imageAssetCache,
      ),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return GameWidget(AnimatedSpriteGame(snapshot.data!));
        } else {
          return Container();
        }
      },
    );
  }

  Widget buildSimpleGameAndWidgets() {
    return Row(
      children: [
        FutureBuilder<Sprite>(
          future: Sprite(
            imagePath: 'boom3.PNG',
            rect: IntRect(left: 512, top: 512, width: 128, height: 128),
            color: Colors.yellowAccent,
            transform: Transform2D(
              anchor: Offset(64.0, 64.0),
            ),
          ).loadImage(imageAssetCache),
          builder: (context, snapshot) {
            return Container(
              width: 300,
              height: 200,
              color: Colors.blueAccent,
              child: Center(
                child: snapshot.hasData
                    ? SpriteWidget(snapshot.data!)
                    : Container(),
              ),
            );
          },
        ),
        FutureBuilder<Game>(
          future: MySimpleGame().initialize(imageAssetCache),
          builder: (context, snapshot) {
            return Container(
              width: 200,
              height: 200,
              color: Colors.redAccent,
              child: snapshot.hasData
                  ? GameWidget(
                      snapshot.data!,
                    )
                  : Container(),
            );
          },
        ),
      ],
    );
  }

  Widget buildGameAndWidgets() {
    return Stack(
      children: [
        FutureBuilder<Game>(
          future: MyPlaygroundGame().initialize(
            imageAssetCache,
            jsonAssetCache,
          ),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return GameWidget(
                snapshot.data!,
              );
            } else {
              return Container();
            }
          },
        ),
        FutureBuilder<Sprite>(
          future: Sprite(
            imagePath: 'AngelBrown.PNG',
            transform: Transform2D(
              translate: Offset(100, 200),
              scale: 2.0,
              rotation: 3.14 / 4.0,
            ),
          ).loadImage(imageAssetCache),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return SpriteWidget(
                snapshot.data!,
              );
            } else {
              return Container();
            }
          },
        ),
        FutureBuilder<AnimatedSprite>(
          future: AnimatedSprite.fromUniformSpriteSheet(
            imagePath: 'boom3.png',
            spriteSize: IntSize(width: 128, height: 128),
            atlasBounds: IntRect(left: 0, top: 0, width: 8, height: 8),
            frameDuration: 0.03,
            color: Colors.transparent,
            transform: Transform2D(
              translate: Offset(0.0, 128 * 2.0),
            ),
          ).loadImages(imageAssetCache),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return GameWidget(
                AnimatedSpriteGame(snapshot.data!),
              );
            } else {
              return Container();
            }
          },
        ),
      ],
    );
  }
}
6
likes
140
pub points
15%
popularity

Publisher

verified publisherapptakk.com

minimal data-driven sprite render and animation lib for Flutter

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

asset_cache, flutter

More

Packages that depend on flim