Home > database >  how to use widget separately in another file in flutter?
how to use widget separately in another file in flutter?

Time:10-02

I want to use appbar widget separately in another file. and call them but not occures error like "The argument type 'Type' can't be assigned to the parameter type 'PreferredSizeWidget?"

how to work arrange this?

enter image description here

CodePudding user response:

You can wrap your widget using PreferredSize or you can use flexibleSpace property in AppBar.

Using PreferredSize

Scaffold(
  appBar: PreferredSize(
    preferredSize: Size.fromHeight(120), // Set this height
    child: Container(
      color: Colors.orange,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('One'),
          Text('Two'),
          Text('Three'),
          Text('Four'),
        ],
      ),
    ),
  ),
)

Using flexibleSpace

Scaffold(
  appBar: AppBar(
    toolbarHeight: 120, // Set this height
    flexibleSpace: Container(
      color: Colors.orange,
      child: Column(
        children: [
          Text('One'),
          Text('Two'),
          Text('Three'),
          Text('Four'),
        ],
      ),
    ),
  ),
)

CodePudding user response:

Your MyAppBar class should have this signature:

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) => AppBar(...); // Your implementation

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

To use it:

Scaffold(
  appBar: MyAppBar(),
  ...
)
  • Related