Is it possible to use Flame's game widget beside flutter's widgets?
171
You need to wrap it in an Expanded
widget (or another widget that gives it a size, SizedBox
for example) the GameWidget
won't be able to be laid out otherwise due to infinite size constraints:
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Sprite examples')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'This a sample text!',
style: TextStyle(fontSize: 22),
),
Expanded(
child: GameWidget(game: SpriteSample()),
)
],
),
),
),
);
}
}
Comments
-
D B over 1 year
In brief, as Flame game is a widget in its own, I want to add that widget inside column widget. The game runs when I add it to runApp(), but when adding widget in column it doesn't. Why?
import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Sprite examples')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'This a sample text!', style: TextStyle(fontSize: 22), ), GameWidget(game: SpriteSample()) //<========== game widget ], ), ), ), ); } } class SpriteSample extends FlameGame { @override Future<void>? onLoad() { add(SpriteOne()); return super.onLoad(); } } class SpriteOne extends PositionComponent { final Paint _paint = Paint()..color = Colors.red; @override Future<void>? onLoad() { return super.onLoad(); } @override void render(Canvas canvas) { canvas.drawCircle(Offset(150, 150), 25, _paint); } @override void update(double dt) { super.update(dt); } }