h-1.flutter.0/lib/widgets/keyboard_inset_wrapper.dart

42 lines
1.2 KiB
Dart

import 'package:flutter/material.dart';
/// Wraps content with SafeArea and animated bottom padding based on keyboard.
/// Use this to keep forms scrollable without Scaffold resizing.
class KeyboardInsetWrapper extends StatelessWidget {
final Widget child;
final EdgeInsets basePadding;
final double extraBottom;
final Duration duration;
final Curve curve;
final bool safeAreaTop;
final bool safeAreaBottom;
const KeyboardInsetWrapper({
super.key,
required this.child,
this.basePadding = EdgeInsets.zero,
this.extraBottom = 0,
this.duration = const Duration(milliseconds: 180),
this.curve = Curves.easeOut,
this.safeAreaTop = true,
this.safeAreaBottom = true,
});
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
final bottomInset = mediaQuery.viewInsets.bottom;
final padding = basePadding.add(EdgeInsets.only(bottom: bottomInset + extraBottom));
final applyBottomSafeArea = safeAreaBottom && bottomInset == 0;
return SafeArea(
top: safeAreaTop,
bottom: applyBottomSafeArea,
child: AnimatedPadding(
duration: duration,
curve: curve,
padding: padding,
child: child,
),
);
}
}