我试图保留倒计时计时器的状态(下面给出的例子),以便当我导航到倒计时页面时,启动倒计时,比如在10,然后暂停它,例如在7,然后导航远离计时器和倒车,计时器显示7,暂停时间,相对于初始状态10。
在单独的代码中(这里没有显示),我尝试使用PageStorageBucket + PageStorage来存储AnimationController,并将其还原为InitState,但这是行不通的。
在离开/返回到它所在的页面后,恢复动画控制器状态的最佳方法是什么?
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),
),
),
),
);
}
}发布于 2019-11-19 19:00:03
保持控制器状态的一种方法是使用提供程序。
创建一个类来存储控制器状态,例如,
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',然后在您的_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(完整代码为这里
https://stackoverflow.com/questions/58775671
复制相似问题