我正在尝试在Flutter应用中显示以下耀斑动画
https://www.2dimensions.com/a/pollux/files/flare/smiley-switch
我将其下载为二进制文件
FlareActor(
"assets/Smiley Switch.flr",
color: Colors.black,
fit: BoxFit.contain,
animation: "On",
),
我希望动画像开关小部件一样从打开切换到关闭,因此我将其包裹在InkWell
内
var a = false;
return Center(
child: InkWell(
onTap: (){
if(a){
setState(() {
a = false;
});
} else {
setState(() {
a = true;
});
}
},
child: FlareActor(
"assets/Smiley Switch.flr",
color: Colors.black,
fit: BoxFit.contain,
animation: a ? "On" : "Off",
),
),
);
,但是上面的代码不起作用。如何切换FlareActor
的动画?
答案 0 :(得分:1)
import "package:flare_flutter/flare_actor.dart";
import "package:flutter/material.dart";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flare Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: MyHomePage(title: 'Flare-Flutter'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isOn = false;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: InkWell(
onTap: () {
setState(() {
isOn = !isOn;
});
},
child: FlareActor(
"assets/flare/SmileySwitch.flr",
alignment: Alignment.center,
fit: BoxFit.contain,
animation: isOn ? "On" : "Off",
),
))
],
),
));
}
}