From f52667ccea72a9eea3c4ab426bbe8c586ec3c8a8 Mon Sep 17 00:00:00 2001 From: Jonas Wanke Date: Tue, 24 Jan 2023 16:29:56 +0100 Subject: [PATCH] Add CenterOrScroll --- lib/black_hole_flutter.dart | 2 +- .../{expand_or_scroll.dart => or_scroll.dart} | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) rename lib/src/widgets/{expand_or_scroll.dart => or_scroll.dart} (58%) diff --git a/lib/black_hole_flutter.dart b/lib/black_hole_flutter.dart index 96db06e..352247d 100644 --- a/lib/black_hole_flutter.dart +++ b/lib/black_hole_flutter.dart @@ -7,6 +7,6 @@ export 'src/navigation.dart'; export 'src/render_object.dart'; export 'src/size.dart'; export 'src/widgets/buttons.dart'; -export 'src/widgets/expand_or_scroll.dart'; export 'src/widgets/fill_or_wrap.dart'; +export 'src/widgets/or_scroll.dart'; export 'src/widgets/title_and_subtitle.dart'; diff --git a/lib/src/widgets/expand_or_scroll.dart b/lib/src/widgets/or_scroll.dart similarity index 58% rename from lib/src/widgets/expand_or_scroll.dart rename to lib/src/widgets/or_scroll.dart index b1467e0..54473dd 100644 --- a/lib/src/widgets/expand_or_scroll.dart +++ b/lib/src/widgets/or_scroll.dart @@ -1,5 +1,40 @@ import 'package:flutter/widgets.dart'; +/// Centers [child] if it's smaller than the parent and makes it scrollable +/// along [scrollDirection] if it's taller. +class CenterOrScroll extends StatelessWidget { + const CenterOrScroll({ + super.key, + this.wrapInSafeArea = true, + this.padding = EdgeInsets.zero, + this.scrollDirection = Axis.vertical, + this.scrollController, + required this.child, + }); + + final bool wrapInSafeArea; + final EdgeInsets padding; + final Axis scrollDirection; + final ScrollController? scrollController; + final Widget child; + + @override + Widget build(BuildContext context) { + var child = this.child; + + if (wrapInSafeArea) child = SafeArea(child: child); + + child = SingleChildScrollView( + padding: padding, + scrollDirection: scrollDirection, + controller: scrollController, + child: child, + ); + + return Center(child: _intrinsicAlongAxis(scrollDirection, child)); + } +} + class ExpandOrScroll extends StatelessWidget { const ExpandOrScroll({ super.key,