Home > Software engineering >  Want to change app bar with other widgets in flutter?
Want to change app bar with other widgets in flutter?

Time:09-10

Want to make blank app bar for one screen instead of app bar with logo title depending on the situation (mainly when using bottom navigation). But appBar() can't be replace with container() widget. How should I do?

Eg.. If bottom navigation bar Index become 0, app bar will disappear although for other Index app bar will appear.

Below is the code snippet with logo appbar

appBar: AppBar(
        centerTitle: true,
        title: Image.asset(
          'assets/fblogosmall300.png',
          fit: BoxFit.contain,
          height: 45,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: <Color>[secondColor, mainColor])),
        ),
      ),

What I expected is, but completely get error.

appBar: currentIndex == 4? Container():AppBar(
        centerTitle: true,
        title: Image.asset(
          'assets/fblogosmall300.png',
          fit: BoxFit.contain,
          height: 45,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: <Color>[secondColor, mainColor])),
        ),
      ),

CodePudding user response:

If you want to remove the app bar just set the value at null.

appBar: currentIndex == 4 ? null : AppBar(
        centerTitle: true,
        title: Image.asset(
          'assets/fblogosmall300.png',
          fit: BoxFit.contain,
          height: 45,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: <Color>[secondColor, mainColor])),
        ),
      ),

Or you can used PreferredSize widget like this

appBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0),
        child: currentIndex == 4 ? Container() : AppBar(),
    ),
    
  • Related