【发布时间】:2019-08-17 05:50:58
【问题描述】:
我正在尝试将曲线类动画“Curves.bounceOut”添加到使用 AnimationController 的代码中。
这是我的代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
var squareScale = 1.0;
static AnimationController _controller;
@override
void initState() {
_controller = AnimationController(
vsync: this,
lowerBound: 0.5,
upperBound: 1.0,
duration: Duration(milliseconds: 300));
_controller.addListener(() {
setState(() {
squareScale = _controller.value;
});
});
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Bounce Example"),
),
body: Stack(
children: <Widget>[
Container(
width: 150.0,
height: 150.0,
color: Colors.yellowAccent,
),
Column(
children: <Widget>[
Row(
children: <Widget>[
GestureDetector(
onTap: () {
_controller.forward(from: 0.0);
},
child: Transform.scale(
scale: squareScale,
child: Container(
width: 150.0,
height: 150.0,
color: Colors.green,
),
),
),
],
),
],
),
],
),
);
}
}
目前,绿色容器的动画从 0.5 缩放到 1.0 缩放,但不会反弹。如何添加“Curves.bounceOut”动画,以便容器在点击时弹跳?
提前感谢您的帮助!
【问题讨论】:
-
见
CurvedAnimation -
我已经看到了 CurvedAnimation 类,但我不知道如何将它正确地包含在我的代码中。
-
而不是
_controller.addListener使用_curvedAnimation.addListener?
标签: dart flutter flutter-animation