animation - 可拖动的 Flutter LinearGradient 动画
问题描述
我有一个带有以下装饰的可拖动容器。
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [ThemeColors.red, ThemeColors.yellow, ThemeColors.green])
我想让它动画我的框架变得更绿或更红,进一步向左或向右拖动。
解决方案
这是一个简单的示例,用于检测渐变内颜色之间的水平拖动和变化:
class GradientScreen extends StatefulWidget {
@override
_GradientScreenState createState() => _GradientScreenState();
}
class _GradientScreenState extends State<GradientScreen> {
var percentage = 0.0;
@override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: Text('Animated Drag Gradient'),
centerTitle: true,
),
body: GestureDetector(
onHorizontalDragUpdate: (details) {
setState(() => percentage = (details.localPosition.dx - 0) / (width - 0));
},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
_colorTween(Colors.green[900], Colors.red[900]),
Colors.yellow,
_colorTween(Colors.green[900], Colors.red[900])
],
)
),
),
),
);
}
Color _colorTween(Color begin, Color end) {
return ColorTween(begin: begin, end: end).transform(percentage);
}
}
这种简单实现的结果如下:
推荐阅读
- javascript - 如何从位于身份验证层后面的 API 获得响应?
- mathjax - 使用 Mathjax 对齐方程
- java - 而那些不是员工的人 - 我如何确保它不会在 executeQuery 中出错?
- laravel - 如何使用ckeditor解决laravel错误405
- r - 使用存储的反应值重命名添加到对象的值
- angular - 防止 Angular 6 中的分层依赖注入
- c++ - Pthread 创建失败
- c# - 从 github repo 构建 SDK
- indexing - 检索从 CGAL 的 surface_neighbor_coordinates_3 返回的最近表面点的索引
- php - 解决PHP中的一个单词“a?