0

Before null-safety I could do:

class FooBar extends PreferredSize {
  final String data;
  FooBar(this.data);

  @override
  Size get preferredSize => Size.fromHeight(100);

  @override
  Widget build(BuildContext context) {
    return DashingText(data); // My own implementation. 
  }
}

and use it like:

Scaffold(
  appBar: FooBar('My App bar'),
)

But with null safety I'm getting an error that I need to provide child to the super class. If I have to provide a child to the super class then what's the benefit of overriding the build method of the super class? Is PreferredSize not an obsolete class after the arrival of null safety?

iDecode
  • 22,623
  • 19
  • 99
  • 186

1 Answers1

2

PreferredSize widget's constructor require 2 parameters which are Widget child and Size preferredSize by making a class that extends this widget you need to comply to its constructor. In its documentation PreferredSize is described like this:

A widget with a preferred size.

This widget does not impose any constraints on its child, and it doesn't affect the child's layout in any way. It just advertises a preferred size which can be used by the parent.

Parents like Scaffold use PreferredSizeWidget to require that their children implement that interface. To give a preferred size to an arbitrary widget so that it can be used in a child property of that type, this widget, PreferredSize, can be used.

Widgets like AppBar implement a PreferredSizeWidget, so that this PreferredSize widget is not necessary for them.

As said above your implementation should looks more like a StatelessWidget implementing a PreferredSizeWidget in the same way it is described for AppBar:

class MyWidget extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello, World!');
  }
  
  @override
  Size get preferredSize => Size.fromHeight(100);
}

Edit: Sample with PreferredSize

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: const Size.fromHeight(80.0),
        child: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: <Color>[Colors.blue, Colors.pink],
            ),
          ),
          child: const AppBarContent(),
        ),
      ),
      body: const Center(
        child: Text('Content'),
      ),
    );
  }
}

AppBar implementation from Flutter source

class AppBar extends StatefulWidget implements PreferredSizeWidget {
  // ...
}
Guillaume Roux
  • 6,352
  • 1
  • 13
  • 38
  • I'm already aware of `extends StatelessWidget implements PreferredSizeWidget` which `PreferredSize` also is, and sorry your post doesn't answer my question. Can you put an example of `PreferredSize`? – iDecode May 29 '21 at 14:30
  • 1
    I've edited my answers with the code sample for `PreferredSize` which you can find in the Flutter documentation. What I meant with my answer was that you were using `PreferredSize` in the wrong way based on the code sample you have provided. You can find the base purpose of the widget `PreferredSize` described in its documentation and in my answer quotation. – Guillaume Roux May 29 '21 at 14:48
  • If your purpose with your code sample was to create a custom appBar then you can wrap your custom widget with `PreferredSize` or implement `PreferredSizeWidget` on it. You were asking the purpose of using `PreferredSize`, well it is (quoting the documentation here): "Advertises a preferred size which can be used by the parent." – Guillaume Roux May 29 '21 at 15:00
  • I think you didn't read the question carefully. My code was working fine until null safety but now it is not working (which I already know why). The question wasn't how to make it work or why there is an error but is `PreferredSize` widget obsolete now? – iDecode May 29 '21 at 15:01
  • 1
    I've read your question but it wasn't really clear with the code you've added. Now is the `PreferredSize` widget annotated as `deprecated` or is it mentionned anywhere in its documentation that it is obsolete ? No. While you might not need to use it often, other developers might find use in this widget. Saying that it is obsolete would be the same as saying the `Padding` widget is obsolete because `Container` has a padding property. We cannot be sure how developers will use `PreferredSize` but maybe in some cases it might be more relevant than implementing `PreferredSizeWidget` for them. – Guillaume Roux May 29 '21 at 15:11