-
-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Adding GameWidget.controlled (#1650)
* feat: Adding GameWidget.controlled * follow up * Update doc/flame/game.md * follow up * Update doc/flame/game.md Co-authored-by: Lukas Klingsbo <me@lukas.fyi> * Update packages/flame/lib/src/game/game_widget/game_widget.dart Co-authored-by: Lukas Klingsbo <me@lukas.fyi> * Update according to comments * Move out GameWidget docs * Update game widget docs * Update docs * Update GameWidget.controlled example * Update .controlled dartdocs * Updated .controlled dartdocs * Add example to game_widget.md * Apply suggestions from code review Co-authored-by: Renan <6718144+renancaraujo@users.noreply.github.com> Co-authored-by: Renan Araujo <renan.araujo@verygood.ventures> Co-authored-by: Renan <6718144+renancaraujo@users.noreply.github.com> Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
- Loading branch information
1 parent
4ca65f8
commit 7ef6a51
Showing
6 changed files
with
491 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# GameWidget | ||
|
||
The `GameWidget` is a Flutter `Widget` that is used to insert a [`Game`](game.md) inside the Flutter | ||
widget tree. | ||
|
||
It can directly receive a `Game` instance in its default constructor or it can receive a | ||
`GameFactory` function on the `controlled` constructor that will be used to create the game once the | ||
`GameWidget` is inserted in the widget tree. | ||
|
||
## Examples | ||
|
||
Directly in `runApp`, with either: | ||
|
||
```dart | ||
void main() { | ||
final game = MyGame(); | ||
runApp(GameWidget(game: game)); | ||
} | ||
``` | ||
|
||
Or: | ||
|
||
```dart | ||
void main() { | ||
runApp(GameWidget.controlled(gameFactory: MyGame.new)); | ||
} | ||
``` | ||
|
||
In a `StatefulWidget`: | ||
|
||
```dart | ||
class MyGamePage extends StatefulWidget { | ||
@override | ||
State createState() => _MyGamePageState(); | ||
} | ||
class _MyGamePageState extends State<MyGamePage> { | ||
late final MyGame _game; | ||
@override | ||
void initState() { | ||
super.initState(); | ||
_game = MyGame(); | ||
} | ||
@override | ||
void build(BuildContext context) { | ||
return GameWidget(game: _game); | ||
} | ||
} | ||
``` | ||
|
||
In a `StatelessWidget` with the `gameFactory` argument: | ||
|
||
```dart | ||
class MyGamePage extends StatelessWidget { | ||
@override | ||
void build(BuildContext context) { | ||
return GameWidget.controlled(gameFactory: MyGame.new); | ||
} | ||
} | ||
``` | ||
|
||
Do note that if the `GameWidget.controlled` constructor is used, the `GameWidget.game` field will | ||
always be null. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.