flutterdartpreferredsize

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


Error message:

@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