flutter - Flutter 为 ScaleTransition 设置自定义限制
问题描述
ScaleTransition
在这个示例代码中,当我尝试zoom-in
或缩放图标时,我想将图标大小限制为 10% zoom-out
,我不能这样做来限制图标
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';
void main() => runApp(MyApp());
class ScaleTransitionExample extends StatefulWidget {
_ScaleTransitionExampleState createState() => _ScaleTransitionExampleState();
}
class _ScaleTransitionExampleState extends State<ScaleTransitionExample> with TickerProviderStateMixin {
AnimationController _controller;
Animation<double> _animation;
initState() {
super.initState();
_controller = AnimationController(duration: const Duration(milliseconds: 2000), vsync: this);
_animation = CurvedAnimation(parent: _controller, curve: Curves.ease);
_controller.forward();
}
@override
dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Stack(
children: <Widget>[
Center(
child: ScaleTransition(
scale: _animation,
alignment: Alignment.center,
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Icon(Icons.check, size: 100.0, color: Colors.green),
])),
),
Align(
alignment: Alignment.bottomLeft,
child:RaisedButton(
child: Text('forward'),
onPressed: ()=>_controller.forward(),
),
),
Align(
alignment: Alignment.bottomRight,
child:RaisedButton(
child: Text('reverse'),
onPressed: ()=>_controller.reverse(),
),
),
],
));
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListView Example',
home: ScaleTransitionExample(),
);
}
}
解决方案
你只需要Tween
.
Tween<double> _tween = Tween(begin: 0.1, end: 1);
完整示例:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListView Example',
home: ScaleTransitionExample(),
);
}
}
class ScaleTransitionExample extends StatefulWidget {
_ScaleTransitionExampleState createState() => _ScaleTransitionExampleState();
}
class _ScaleTransitionExampleState extends State<ScaleTransitionExample> with TickerProviderStateMixin {
AnimationController _controller;
Tween<double> _tween = Tween(begin: 0.1, end: 1);
initState() {
super.initState();
_controller = AnimationController(duration: const Duration(milliseconds: 1000), vsync: this);
_controller.forward();
}
@override
dispose() {
_controller.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Stack(
children: <Widget>[
Center(
child: ScaleTransition(
scale: _tween.animate(CurvedAnimation(parent: _controller, curve: Curves.ease)),
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.check, size: 100.0, color: Colors.green),
],
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: RaisedButton(
child: Text('forward'),
onPressed: () => _controller.forward(),
),
),
Align(
alignment: Alignment.bottomRight,
child: RaisedButton(
child: Text('reverse'),
onPressed: () => _controller.reverse(),
),
),
],
),
);
}
}
推荐阅读
- windows - Windows Docker Image 在 Azure 上不断挂起(网络应用程序)
- c++ - 使用不同方法计算函数时的不同结果 y = f(x)
- c++ - 创建一个“空”进程
- reactjs - SVG 线上的 Framer 运动长度过渡
- gitlab - 在 GitLab.com 上导入自托管 GitLab 导出时,如何保留创建者/分配/评论者关联?
- python - python脚本完成后如何保持运行?
- powershell - 如何在使用 az monitor app-insights 查询时运行多行查询?
- c - 如何通过将两个矩阵相乘来解决这个问题?
- linux - 我想删除带有前缀“vault-”且不是最新创建的用户的脚本的 IAM 用户
- javascript - 如何刷新 DIV 并立即滚动到底部?