Created
October 4, 2023 04:42
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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