sheet_media_query.dart
1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import 'package:flutter/widgets.dart';
import 'package:sheet/sheet.dart';
import 'dart:math' as math;
/// A widget that updates the top safe area proportionally to the position
/// of the sheet
///
/// If the sheet is below the top safe area the inner top padding will be 0.
/// Once the sheet enters the top safe area the inner top padding will increase
/// proportionally to the sheet offset.
/// If the sheet is fully expanded to the top of the screen the top padding
/// will be the same as the parent top safe area.
class SheetMediaQuery extends StatelessWidget {
const SheetMediaQuery({Key? key, required this.child}) : super(key: key);
final Widget child;
@override
Widget build(BuildContext context) {
final controller = Sheet.of(context)!.controller;
final MediaQueryData data = MediaQuery.of(context);
return LayoutBuilder(builder: (context, constraints) {
return AnimatedBuilder(
animation: controller.animation,
builder: (BuildContext context, Widget? child) {
final position = controller.position;
final viewportDimension = position.hasViewportDimension
? position.viewportDimension
: double.infinity;
final pixels = position.hasPixels ? position.pixels : 0;
final offset = viewportDimension - pixels;
final topPadding = math.max(0.0, data.padding.top - offset);
return MediaQuery(
data: data.copyWith(
padding: data.padding.copyWith(
top: topPadding,
),
),
child: child!,
);
},
child: child,
);
});
}
}