AnimatedWidget的子类,使用Offset类型的动画器让子组件在两个Offset对象之间进行过渡动画。

SlideTransition基本使用

  1. <br />【child】 : 孩子组件 【Widget】<br />【textDirection】 : x轴方向 【TextDirection】<br />【position】 : 动画 【Animation<Offset><br />![140.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589457239442-e37cb41d-e73f-47b5-b66e-9a69a4a96ad6.gif#align=left&display=inline&height=226&margin=%5Bobject%20Object%5D&name=140.gif&originHeight=226&originWidth=804&size=181850&status=done&style=none&width=804)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class CustomSlideTransition extends StatefulWidget {
  @override
  _CustomSlideTransitionState createState() => _CustomSlideTransitionState();
}

class _CustomSlideTransitionState extends State<CustomSlideTransition>
    with SingleTickerProviderStateMixin {
  AnimationController _ctrl;

  @override
  void initState() {
    _ctrl = AnimationController(vsync: this, duration: Duration(seconds: 1));
    _ctrl.forward();
    super.initState();
  }

  @override
  void dispose() {
    _ctrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () {
          setState(() {
            _ctrl.reset();
            _ctrl.forward();
          });
        },
        child: Container(
          width: MediaQuery.of(context).size.width,
          color: Colors.grey.withAlpha(33),
          height: 100,
          child: SlideTransition(
            textDirection: TextDirection.ltr,
            position: Tween<Offset>(
              begin: Offset.zero,
              end: Offset(0.2, 0.2),
            ).animate(_ctrl),
            child: Container(
                child: Icon(Icons.android, color: Colors.green, size: 60)),
          ),
        ));
  }
}