- Создайте
GlobalKey
и AnimationController
для каждого из ваших виджетов.
- Используйте
ScrollController
для прослушивания событий прокрутки в представлении прокрутки (не обязательно должно быть ListView
)
- Используйте
GlobalKey.currentContext.findRenderObject()
, чтобы получить ссылку на фактический объект, отображаемый на экране.
- Если
RenderObject
существует, найдите его относительное положение (getTransformTo
) и проверьте, видна ли позиция в представлении прокрутки
- Запустить анимацию. Чтобы убедиться, что он срабатывает только один раз, проверьте
AnimationStatus
из AnimationController
.
* * Пример 1 022
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Playground',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new TestPage(),
);
}
}
class TestPage extends StatefulWidget {
@override
_TestPageState createState() => new _TestPageState();
}
class _TestPageState extends State<TestPage> with SingleTickerProviderStateMixin {
final listViewKey = new GlobalKey();
final animatedBoxKey = new GlobalKey();
final scrollController = new ScrollController();
AnimationController animatedBoxEnterAnimationController;
@override
void initState() {
super.initState();
animatedBoxEnterAnimationController = new AnimationController(
vsync: this,
duration: Duration(milliseconds: 2000),
);
scrollController.addListener(() {
_updateAnimatedBoxEnterAnimation();
});
}
static const enterAnimationMinHeight = 100.0;
_updateAnimatedBoxEnterAnimation() {
if (animatedBoxEnterAnimationController.status != AnimationStatus.dismissed) {
return; // animation already in progress/finished
}
RenderObject listViewObject = listViewKey.currentContext?.findRenderObject();
RenderObject animatedBoxObject = animatedBoxKey.currentContext?.findRenderObject();
if (listViewObject == null || animatedBoxObject == null) return;
final listViewHeight = listViewObject.paintBounds.height;
final animatedObjectTop = animatedBoxObject.getTransformTo(listViewObject).getTranslation().y;
final animatedBoxVisible = (animatedObjectTop + enterAnimationMinHeight < listViewHeight);
if (animatedBoxVisible) {
// start animation
animatedBoxEnterAnimationController.forward();
}
}
@override
Widget build(BuildContext context) {
final boxOpacity = CurveTween(curve: Curves.easeOut).animate(animatedBoxEnterAnimationController);
final boxPosition = Tween(begin: Offset(-1.0, 0.0), end: Offset.zero)
.chain(CurveTween(curve: Curves.elasticOut))
.animate(animatedBoxEnterAnimationController);
return new Scaffold(
appBar: new AppBar(
title: new Text('Flutter Playground'),
),
body: new ListView(
key: listViewKey,
controller: scrollController,
children: <Widget>[
new Container(
padding: EdgeInsets.all(16.0),
child: new Text(
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
style: TextStyle(fontSize: 24.0),
),
),
new FadeTransition(
opacity: boxOpacity,
child: new SlideTransition(
position: boxPosition,
child: new Container(
key: animatedBoxKey,
height: 300.0,
color: Colors.green,
padding: EdgeInsets.all(16.0),
child: new Text('Animated Box'),
),
),
),
new Container(
padding: EdgeInsets.all(16.0),
child: new Text(
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
style: TextStyle(fontSize: 24.0),
),
),
new FlatButton(
onPressed: () {
scrollController.jumpTo(0.0);
animatedBoxEnterAnimationController.reset();
},
child: new Text('Reset'),
)
],
),
);
}
}