Skip to content

Instantly share code, notes, and snippets.

@SamJakob
Created October 4, 2023 04:42
Show Gist options
  • Save SamJakob/9bd17ff8ce91ade6bf273a07b546b98e to your computer and use it in GitHub Desktop.
Save SamJakob/9bd17ff8ce91ade6bf273a07b546b98e to your computer and use it in GitHub Desktop.
Ever wished that SizedBox implements PreferredSizeWidget? Use this to bridge the gap between SizedBox and PreferredSizedWidget!
/// This class extends [SizedBox] whilst implementing [PreferredSizeWidget]
/// based on the defined size. This allows the use of [SizedBox] in places
/// where [PreferredSizeWidget] is required, such as in [AppBar.preferredSize].
class PreferredSizedBox extends SizedBox implements PreferredSizeWidget {
const PreferredSizedBox({super.key});
const PreferredSizedBox.shrink({super.key, super.child}) : super.shrink();
const PreferredSizedBox.expand({super.key, super.child}) : super.expand();
PreferredSizedBox.fromSize({super.key, super.child, super.size})
: super.fromSize();
const PreferredSizedBox.square({super.key, super.child, super.dimension})
: super.square();
@override
Size get preferredSize => Size(
width ?? double.infinity,
height ?? double.infinity,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment