{"id":15498074,"url":"https://github.com/rodydavis/flutter_implicit_animations","last_synced_at":"2025-04-22T22:14:55.035Z","repository":{"id":45755394,"uuid":"514081646","full_name":"rodydavis/flutter_implicit_animations","owner":"rodydavis","description":"Flutter animations every frame","archived":false,"fork":false,"pushed_at":"2022-07-16T00:02:46.000Z","size":6201,"stargazers_count":37,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-22T22:14:32.977Z","etag":null,"topics":["animation","flutter","game-logic","game-loop","games"],"latest_commit_sha":null,"homepage":"https://rodydavis.github.io/flutter_implicit_animations/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rodydavis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-15T00:07:03.000Z","updated_at":"2022-07-24T03:30:15.000Z","dependencies_parsed_at":"2022-07-26T15:17:56.074Z","dependency_job_id":null,"html_url":"https://github.com/rodydavis/flutter_implicit_animations","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodydavis%2Fflutter_implicit_animations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodydavis%2Fflutter_implicit_animations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodydavis%2Fflutter_implicit_animations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodydavis%2Fflutter_implicit_animations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodydavis","download_url":"https://codeload.github.com/rodydavis/flutter_implicit_animations/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250331823,"owners_count":21413103,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["animation","flutter","game-logic","game-loop","games"],"created_at":"2024-10-02T08:41:49.032Z","updated_at":"2025-04-22T22:14:55.006Z","avatar_url":"https://github.com/rodydavis.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flutter implicit animations\n\n[Live Demo](https://rodydavis.github.io/flutter_implicit_animations/)\n\n![](/screenshots/cubes.png)\n\n## Problem\n\nThere are a few options with flutter when creating animations:\n\n- AnimationController\n- AnimatedWidget and other AnimatedX classes\n- Animations [package](https://pub.dev/packages/animations)\n\nThis solutions all solve a variety of problems and can solve certain use cases.\n\nWhen building animations you want to do a piece of work every frame and animated it\n\nThe build method already does a great job of rendering the state synchronously and will cache widgets that have not changed.\n\n## Solution\n\nThis example aims to build animations in a game-like loop with a update, paint and start callback.\n\n```dart\nclass SimpleExample extends StatefulWidget {\n  const SimpleExample({Key? key}) : super(key: key);\n\n  @override\n  State\u003cSimpleExample\u003e createState() =\u003e _SimpleExampleState();\n}\n\nclass _SimpleExampleState extends AnimatedState\u003cSimpleExample\u003e {\n  var x = 0.0;\n  var y = 0.0;\n  var z = 0.0;\n\n  @override\n  void update(Duration time) {\n    final t = delta.inMilliseconds / 1000;\n    x += t;\n    y += t;\n    z += t;\n  }\n\n  @override\n  Widget paint(BuildContext context, BoxConstraints constraints) {\n    return Material(\n      child: Center(\n        child: Container(\n          width: 100,\n          height: 100,\n          transform: Matrix4.identity()\n            ..rotateX(x)\n            ..rotateY(y)\n            ..rotateZ(z),\n          child: const Text(\n            'Hello World',\n            style: TextStyle(\n              fontSize: 30,\n              fontWeight: FontWeight.bold,\n            ),\n          ),\n        ),\n      ),\n    );\n  }\n}\n```\n\nBy only changing `State` to `AnimatedState` and the build method to paint method with constraints it is possible to draw every frame.\n\nThis also makes it possible to animate multiple things at once and update asynchronously.\n\n## Inline painter\n\nThere is also a helper class I use a lot to render a inline canvas.\n\n```dart\nCustomPaint(\n  painter: InlinePainter(\n    draw: (canvas, size) {\n      final paint = Paint()\n      ..color = cube.color\n      ..style = PaintingStyle.fill\n      ..strokeWidth = 2;\n      final rect = Rect.fromLTWH(0, 0, size.width, size.height);\n      canvas.drawRect(rect, paint);\n    },\n  ),\n)\n```\n\n## Example\n\n```dart\nimport 'dart:math';\n\nimport 'package:flutter/material.dart';\n\nimport '../widgets/animation.dart';\nimport '../widgets/inline_painter.dart';\n\nclass HomeScreen extends StatefulWidget {\n  const HomeScreen({Key? key}) : super(key: key);\n\n  @override\n  State\u003cHomeScreen\u003e createState() =\u003e _HomeScreenState();\n}\n\nclass _HomeScreenState extends AnimatedState\u003cHomeScreen\u003e {\n  final List\u003cCube\u003e cubes = \u003cCube\u003e[];\n\n  double x = 0.0, y = 0.0, z = 0.0;\n  Color color = Colors.red;\n  Offset offset = Offset.zero;\n\n  void addCube(Size size) {\n  final randomOffset = Offset(\n    Random().nextDouble() * size.width,\n    Random().nextDouble() * size.height,\n  );\n  final randomDelta = Random().nextDouble() * 20;\n  final randomDirection = Offset(\n    Random().nextDouble() * 2 - 1,\n    Random().nextDouble() * 2 - 1,\n  );\n  final cube = Cube()\n    ..offset = randomOffset\n    ..delta = randomDelta\n    ..direction = randomDirection;\n  cubes.add(cube);\n  debugPrint('Cube added: $randomOffset');\n  }\n\n  @override\n  void start(Duration time) {\n  final size = MediaQuery.of(context).size;\n  addCube(size);\n  super.start(time);\n  }\n\n  @override\n  void update(Duration time) {\n  for (final cube in cubes) {\n    cube.update(time, constraints.biggest);\n  }\n  }\n\n  @override\n  Widget paint(BuildContext context, BoxConstraints constraints) {\n  return Scaffold(\n    appBar: AppBar(\n    title: const Text('Animation Example'),\n    ),\n    body: Stack(\n    fit: StackFit.expand,\n    children: [\n      for (final cube in cubes)\n      Positioned.fromRect(\n        rect: cube.rect,\n        child: CustomPaint(\n        painter: InlinePainter(\n          draw: (canvas, size) {\n          final paint = Paint()\n            ..color = cube.color\n            ..style = PaintingStyle.fill\n            ..strokeWidth = 2;\n          final rect = Rect.fromLTWH(0, 0, size.width, size.height);\n          canvas.drawRect(rect, paint);\n          },\n        ),\n        ),\n      ),\n    ],\n    ),\n    floatingActionButton: FloatingActionButton.extended(\n    onPressed: () =\u003e addCube(constraints.biggest),\n    icon: const Icon(Icons.add),\n    label: Text('Cubes: ${cubes.length}'),\n    ),\n  );\n  }\n}\n\nclass Cube {\n  Color color = Colors.black;\n  Offset offset = Offset.zero;\n  Size size = const Size(100, 100);\n  double delta = 10;\n  Offset direction = const Offset(0.1, 0.4);\n  Rect get rect =\u003e offset \u0026 size;\n\n  void update(Duration time, Size size) {\n  // Move cube and change direction if out of bounds\n  offset = offset + direction * delta;\n\n  // Top\n  if (offset.dy \u003c 0) {\n    direction = Offset(direction.dx, 1);\n  }\n  // Bottom\n  if (offset.dy \u003e size.height) {\n    direction = Offset(direction.dx, -1);\n  }\n  // Left\n  if (offset.dx \u003c 0) {\n    direction = Offset(1, direction.dy);\n  }\n  // Right\n  if (offset.dx \u003e size.width) {\n    direction = Offset(-1, direction.dy);\n  }\n\n  // Change color\n  color = Color.fromARGB(\n    255,\n    (offset.dx * 255 / size.width).round(),\n    (offset.dy * 255 / size.height).round(),\n    0,\n  );\n  }\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodydavis%2Fflutter_implicit_animations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodydavis%2Fflutter_implicit_animations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodydavis%2Fflutter_implicit_animations/lists"}