代码实验室上有一个很好的例子,关于Flutter中的Material Advanced Components。但是,无论我为 AnimationController 的 duration 更改了什么类型和数量,它都不会影响 PositionedTransition 。 我曾经与 CurvedAnimation 一起工作,并且持续时间很好。 我在这里没有发现任何问题,请给我一些建议。
我尝试了什么:
_controller = AnimationController(
duration: Duration(seconds: 5), value: 1.0, vsync: this);
代码段:
class _BackdropState extends State<Backdrop>
with SingleTickerProviderStateMixin {
final double _kFlingVelocity = 2.0;
AnimationController _controller;
final GlobalKey _backdropKey = GlobalKey(debugLabel: 'Backdrop');
@override
void didUpdateWidget(Backdrop oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.currentCategory != oldWidget.currentCategory) {
_toggleBackdropLayerVisibility();
} else if (!_frontLayerVisible) {
_controller.fling(velocity: _kFlingVelocity);
}
}
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(microseconds: 300), value: 1.0, vsync: this);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
bool get _frontLayerVisible {
final AnimationStatus animationStatus = _controller.status;
return animationStatus == AnimationStatus.completed ||
animationStatus == AnimationStatus.forward;
}
void _toggleBackdropLayerVisibility() {
_controller.fling(
velocity: _frontLayerVisible ? -_kFlingVelocity : _kFlingVelocity);
}
Widget _buildStack(BuildContext context, BoxConstraints constraints) {
const double layerTitleHeight = 48.0;
final Size layerSize = constraints.biggest;
final double layerTop = layerSize.height - layerTitleHeight;
Animation<RelativeRect> layerAnimation = RelativeRectTween(
begin: RelativeRect.fromLTRB(
0.0, layerTop, 0.0, layerTop - layerSize.height),
end: RelativeRect.fromLTRB(0, 0, 0, 0))
.animate(_controller.view);
return Stack(
key: _backdropKey,
children: <Widget>[
ExcludeSemantics(
child: widget.backLayer,
excluding: _frontLayerVisible,
),
PositionedTransition(
rect: layerAnimation,
child: _FrontLayer(
onTap: _toggleBackdropLayerVisibility,
child: widget.frontLayer,
),
)
],
);
}
@override
Widget build(BuildContext context) {
var appBar = AppBar(
brightness: Brightness.light,
elevation: 0.0,
titleSpacing: 0.0,
/* leading: IconButton(
icon: Icon(Icons.menu), onPressed: _toggleBackdropLayerVisibility),*/
title: _BackdropTitle(
listenable: _controller.view,
onPress: _toggleBackdropLayerVisibility,
frontTitle: widget.frontTitle,
backTitle: widget.backTitle),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.search,
semanticLabel: 'login',
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) => LoginPage()));
},
),
IconButton(
icon: Icon(
Icons.tune,
semanticLabel: 'login',
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) => LoginPage()));
},
)
],
);
return Scaffold(
appBar: appBar,
body: LayoutBuilder(builder: _buildStack),
);
}
}