Skip to content

Instantly share code, notes, and snippets.

@SamJakob
Created October 4, 2023 04:42

Revisions

  1. SamJakob created this gist Oct 4, 2023.
    19 changes: 19 additions & 0 deletions PreferredSizedBox.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    /// 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,
    );
    }