constraint_flex_view.dart 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import 'package:flutter/material.dart';
  2. class ConstrainedFlexView extends StatelessWidget {
  3. final Widget child;
  4. final double minSize;
  5. final Axis axis;
  6. final EdgeInsets scrollPadding;
  7. const ConstrainedFlexView(this.minSize,
  8. {Key? key,
  9. required this.child,
  10. this.axis = Axis.horizontal,
  11. this.scrollPadding = EdgeInsets.zero})
  12. : super(key: key);
  13. bool get isHz => axis == Axis.horizontal;
  14. @override
  15. Widget build(BuildContext context) {
  16. return LayoutBuilder(
  17. builder: (_, constraints) {
  18. final viewSize = isHz ? constraints.maxWidth : constraints.maxHeight;
  19. if (viewSize > minSize) return child;
  20. return Padding(
  21. padding: scrollPadding,
  22. child: SingleChildScrollView(
  23. child: ConstrainedBox(
  24. constraints: BoxConstraints(
  25. maxHeight: isHz ? double.infinity : minSize,
  26. maxWidth: isHz ? minSize : double.infinity),
  27. child: child,
  28. ),
  29. ),
  30. );
  31. },
  32. );
  33. }
  34. }