lemmur-app-android/lib/widgets/reveal_after_scroll.dart

42 lines
1.0 KiB
Dart
Raw Normal View History

2021-02-24 20:52:18 +01:00
import 'dart:math' show max, min;
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
/// Makes the child reveal itself after given distance
class RevealAfterScroll extends HookWidget {
final Widget child;
/// distance after which [child] should appear
final int after;
final int transition;
final ScrollController scrollController;
final bool fade;
const RevealAfterScroll({
@required this.scrollController,
@required this.child,
@required this.after,
this.transition = 15,
this.fade = false,
}) : assert(scrollController != null),
assert(child != null),
assert(after != null);
@override
Widget build(BuildContext context) {
useListenable(scrollController);
final scroll = scrollController.position.pixels;
return Opacity(
opacity:
fade ? max(0, min(transition, scroll - after + 20)) / transition : 1,
child: Transform.translate(
offset: Offset(0, max(0, after - scroll)),
child: child,
),
);
}
}