1

So I'm working on an app. Every time when I have to make a new page I always have to make the same appBar from Scratch.

Is it possible to assign this appBar to a constant and use that constant everywhere I need it.?

OR

Is there another way to just have one appBar for the entire app?

codegood
  • 29
  • 5

2 Answers2

1

To make your Custom Appbar you need to implement PreferredSizeWidget because the AppBar itself implements it.

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
final String screenTitle;

MyAppBar({@required this.screenTitle});

@override
Widget build(BuildContext context) {
  return AppBar(
    title: Text(screenTitle),
    actions: // Whatever you need
  );
}

@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
Mahesh
  • 862
  • 5
  • 11
0

Yes, write your own:

class MyAppBar extends StatelessWidget implements PreferredSizeWidget{
  const MyAppBar({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => throw UnimplementedError();
}

The AppBar Widget hasn't a constant constructor so you can't make it const

MCB
  • 503
  • 1
  • 8
  • 21