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 {
// ...
}