AppBar will not take title when you use PreferredSize widget. What is the alternative?

Error message:

  • The named parameter ‘child’ is required, but there’s no corresponding argument
  • The named parameter ‘title’ isn’t defined.
@override
Widget build(BuildContext context) {
    double _w = MediaQuery.of(context).size.width;
        return  Scaffold(
        appBar: PreferredSize(title: Text('hi'); //the problem. How should it be used here?
        preferredSize: const Size.fromHeight(100),
        child: Container(color: Colors.blueGrey),
),

>Solution :

The PreferredSize widget does not have a property called title. You have to use the child property to display the title.

Example:

Widget build(BuildContext context) {
    double _w = MediaQuery.of(context).size.width;
    return  Scaffold(
        appBar: PreferredSize(
          preferredSize: const Size.fromHeight(100),
          child: Container(child: Text('hi')),
        ),

In the child, you can pass any type of widget i.e. Container, Row, Sizedbox, InputField

Leave a Reply