Flutter 返回页面后恢复AnimationController状态
我试图保留倒计时计时器的状态(下面给出的示例),这样当我导航到倒计时计时器页面时,开始倒计时,比如说在10,然后暂停,比如说在7,然后离开计时器返回,计时器显示7,暂停时间,而初始状态为10 在单独的代码(此处未显示)中,我尝试使用PageStorageBucket+PageStorage来存储AnimationController,并在InitState中还原它,但这不起作用 离开/返回动画控制器所在页面后,恢复动画控制器状态的最佳方法是什么Flutter 返回页面后恢复AnimationController状态,flutter,flutter-animation,Flutter,Flutter Animation,我试图保留倒计时计时器的状态(下面给出的示例),这样当我导航到倒计时计时器页面时,开始倒计时,比如说在10,然后暂停,比如说在7,然后离开计时器返回,计时器显示7,暂停时间,而初始状态为10 在单独的代码(此处未显示)中,我尝试使用PageStorageBucket+PageStorage来存储AnimationController,并在InitState中还原它,但这不起作用 离开/返回动画控制器所在页面后,恢复动画控制器状态的最佳方法是什么 import 'package:flutter/m
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
color: Colors.blue,
child: Text("AnimatonController"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Counter()),
);
},
),
],
),
),
);
}
}
class CounterText extends AnimatedWidget {
CounterText({Key key, this.animation})
: super(key: key, listenable: animation);
Animation<int> animation;
@override
build(BuildContext context) {
return new Text(
animation.value.toString(),
style: new TextStyle(fontSize: 200.0),
);
}
}
class Counter extends StatefulWidget {
State createState() => new _CounterState();
}
class _CounterState extends State<Counter> with TickerProviderStateMixin {
AnimationController _controller;
static const int startCount = 10;
@override
void initState() {
super.initState();
_controller = new AnimationController(
vsync: this,
duration: new Duration(seconds: startCount),
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: new FloatingActionButton(
child: Icon(_controller.isAnimating ? Icons.pause : Icons.play_arrow),
onPressed: () {
setState(() {
_controller.isAnimating
? _controller.stop()
: _controller.forward();
});
}),
body: new Container(
child: new Center(
child: new CounterText(
animation: new StepTween(
begin: startCount,
end: 0,
).animate(_controller),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
扁平按钮(
颜色:颜色,蓝色,
子项:文本(“AnimationController”),
已按下:(){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>Counter()),
);
},
),
],
),
),
);
}
}
类CounterText扩展了AnimatedWidget{
计数器文本({Key,this.animation})
:super(关键点:关键点,可收听:动画);
动画;
@凌驾
构建(构建上下文){
返回新文本(
animation.value.toString(),
样式:新的文本样式(fontSize:200.0),
);
}
}
类计数器扩展StatefulWidget{
State createState()=>new_countstate();
}
类_CounterState使用TickerProviderStateMixin扩展状态{
动画控制器_控制器;
静态常数int startCount=10;
@凌驾
void initState(){
super.initState();
_控制器=新的AnimationController(
vsync:这个,,
持续时间:新的持续时间(秒数:startCount),
);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
floatingActionButton:新的floatingActionButton(
子:图标(_controller.isAnimating?Icons.pause:Icons.play_arrow),
已按下:(){
设置状态(){
_控制器。正在初始化
?_控制器。停止()
:_controller.forward();
});
}),
主体:新容器(
孩子:新中心(
子:新的反文本(
动画:新StepTween(
开始:startCount,
完:0,,
).设置动画(_控制器),
),
),
),
);
}
}
保存控制器状态的一种方法是使用
创建一个类来存储控制器状态,例如
class CounterStateModel {
AnimationController _controller;
int startCount = 10;
}
在提供商中包装您的应用程序
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Provider<CounterStateModel>(
builder: (context) => CounterStateModel(),
child: MaterialApp(
title: 'Flutter Demo',
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回提供者(
生成器:(上下文)=>CounterStateModel(),
孩子:MaterialApp(
标题:“颤振演示”,
然后在_CounterState类中,使用此提供程序保存/检索控制器
@override
Widget build(BuildContext context) {
CounterStateModel _counterStateModel = Provider.of<CounterStateModel>(context);
if (_counterStateModel._controller == null) {
_counterStateModel._controller = new AnimationController(
vsync: this,
duration: new Duration(seconds: _counterStateModel.startCount),
);
_counterStateModel._controller = _controller;
} else {
_controller = _counterStateModel._controller;
}
return new Scaffold(
@覆盖
小部件构建(构建上下文){
CounterStateModel\u CounterStateModel=Provider.of(上下文);
if(\u countstateModel.\u controller==null){
_counterStateModel.\u控制器=新的AnimationController(
vsync:这个,,
持续时间:新的持续时间(秒数:\ u countstateModel.startCount),
);
_counterStateModel.\u controller=\u controller;
}否则{
_控制器=\u counterStateModel.\u控制器;
}
归还新脚手架(
完整代码是