是否可以在 flutter 的小部件旁边使用 Flame 的游戏小部件?
Is it possible to use Flame's game widget beside flutter's widgets?
简而言之,由于 Flame 游戏本身就是一个小部件,我想将该小部件添加到列小部件中。当我将它添加到 runApp() 时游戏运行,但是当在列中添加小部件时它不会。为什么?
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);
}
}
你需要将它包裹在一个 Expanded
小部件(或另一个给它一个大小的小部件,例如 SizedBox
)中 GameWidget
将无法放置否则由于无限大小限制:
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()),
)
],
),
),
),
);
}
}
简而言之,由于 Flame 游戏本身就是一个小部件,我想将该小部件添加到列小部件中。当我将它添加到 runApp() 时游戏运行,但是当在列中添加小部件时它不会。为什么?
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);
}
}
你需要将它包裹在一个 Expanded
小部件(或另一个给它一个大小的小部件,例如 SizedBox
)中 GameWidget
将无法放置否则由于无限大小限制:
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()),
)
],
),
),
),
);
}
}