How do you reduce the height of the Cupertino Sliver Navigation bar to make it look like an ordinary Cupertino Navigation Bar with a scrollable body

Issue

     @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          child: CustomScrollView(
            slivers: <Widget>[
              CupertinoSliverNavigationBar(
                largeTitle: Container(height:0.0),
                trailing:
                IconButton(
                  icon: Icon(
                    IconData(0xe900, fontFamily: 'message6'),
                    color: Colors.black,
                    size: 25.0,
                  ),
                  onPressed: () {
                   // Navigator.pushNamed(context, '/chat');
                  },
                ),


                middle: Text('Search',style: TextStyle(fontSize: 15.5,
                ),)

              ),


            ],

          ),
        );
      }
    }

Am trying to get rid of the collapsible space in the CupertinoSliverNavigationBar in the code snippet. I do not want the largeTitle property to be there but unfortunately the CupertinoSliverNavigationBar requires it. Thus I have worked around it by putting an empty Container widget with a height of 0.0 like you would set expandable height to 0.0 in a sliver App Bar for an android design to give it a non expandable height. However, for The CupertinoSliverNavigationBar it doesn’t work as it leaves an empty expandable space. I am trying to achieve a slim Cupertino styled Navigation Bar. I can’t use CupertinoNavigationBar because I am using a CustomScrollView widget because of scrolling content and flutter requires the CupertinoSliverNavigationBar in such a use case.

Does anyone know a work around this constraint? I want a slim bar/non-collapsible with scrollable content in the body.

Solution

You can use –SliverPersistentHeader to use – CupertinoNavigationBar

child: CustomScrollView(
          slivers: <Widget>[
            SliverPersistentHeader(
              delegate: MyNav(),
              pinned: true,
              floating: false,
            ),

  class MyNav extends SliverPersistentHeaderDelegate {
  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    // TODO: implement build
    return CupertinoNavigationBar(
      middle: Text(
        'Search',
        style: TextStyle(
          fontSize: 15.5,
        ),
      ),
      trailing: Material(
        child: IconButton(
          icon: Icon(
            IconData(0xe900, fontFamily: 'message6'),
            color: Colors.black,
            size: 25.0,
          ),
          onPressed: () {
            // Navigator.pushNamed(context, '/chat');
          },
        ),
      ),
    );
  }

  @override
  // TODO: implement maxExtent
  double get maxExtent => 60.0;

  @override
  // TODO: implement minExtent
  double get minExtent => 60.0;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    // TODO: implement shouldRebuild
    return false;
  }
}

Other option is to use – SliverToBoxAdapter widget. This way CupertinoNavigationBar is not pinned at top.

child: CustomScrollView(
          slivers: <Widget>[
            SliverToBoxAdapter(
              child: CupertinoNavigationBar(
                middle: Text(
                  'Search',
                  style: TextStyle(
                    fontSize: 15.5,
                  ),
                ),
                trailing: Material(
                  child: IconButton(
                    icon: Icon(
                      IconData(0xe900, fontFamily: 'message6'),
                      color: Colors.black,
                      size: 25.0,
                    ),
                    onPressed: () {
                      // Navigator.pushNamed(context, '/chat');
                    },
                  ),
                ),
              ),
            ),

Answered By – anmol.majhail

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *