Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set minimum height of SliverAppBar within NestedScrollView in Flutter

I'd like to use SliverAppBar within NestedScrollView in an ordinary Scaffold app. I also would like to have some minimum height of the app bar when user scrolls down.

I can't figure out how to use either PreferredSize widget nor any other solution found online e.g. this.

Here is my current simplified solution:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue, fontFamily: 'Oswald'),
      home: SliverHome(),
    );
  }
}

class SliverHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: testHome(),
    );
  }

  Widget testHome() {
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar( // this is where I would like to set some minimum constraint
              expandedHeight: 300,
              floating: false,
              pinned: true,
              flexibleSpace: Container(
                padding: EdgeInsets.all(10),
                height: 340,
                width: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      height: 40,
                    ),
                    Container(
                      height: 60,
                    ),
                    Expanded(child: Container()),
                    Text('TEST'),
                  ],
                ),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage('https://picsum.photos/400/400'),
                        fit: BoxFit.cover)),
              ),
            )
          ];
        },
        body: Container(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => {},
        tooltip: '+',
        child: Icon(Icons.accessibility_new),
      ),
    );
  }
}

I'd like to stop shrinking of the app bar somewhere near 60 dp

screenshot

like image 811
Dominik Roszkowski Avatar asked Feb 01 '19 08:02

Dominik Roszkowski


People also ask

What is default AppBar height flutter?

AppBar has a fixed height of 56.

How do you use SliverAppBar in flutter?

SliverAppBar is a Material Design widget in flutter which gives scrollable or collapsible app-bar. The word Sliver is given to scrollable areas here. SliverAppBar basically gives us means to create an app-bar that can change appearance, blend in the background, or even disappear as we scroll.

How do you add height and width to AppBar in flutter?

Creating AppBarpreferredSize: it takes the Size property and you can then choose fromHeight method to give extra height to your AppBar. child: the child widget which is AppBar.

What is Toolbar height in flutter?

toolbarHeight. Defines the height of the toolbar component of an AppBar.


Video Answer


3 Answers

Simply use collapsedHeight and specify your minimum height.

 SliverAppBar(
  // Display a placeholder widget to visualize the shrinking size.
  flexibleSpace: Placeholder(),
  expandedHeight: 300,
  collapsedHeight: 60,
);
like image 71
Alexandru Mariuti Avatar answered Nov 01 '22 19:11

Alexandru Mariuti


I don't know since exactly when, but as of now, you can also use the collapsedHeight property, documented as below:

https://api.flutter.dev/flutter/material/SliverAppBar/collapsedHeight.html

Currently I am on flutter 1.20.1 and I have this property available.

like image 33
abhay Avatar answered Nov 01 '22 17:11

abhay


This is Actually an requested Feature - https://github.com/flutter/flutter/issues/21298

A work Around is to use Bottom

SliverAppBar(
              // this is where I would like to set some minimum constraint
              expandedHeight: 300,
              floating: false,
              pinned: true,
              bottom: PreferredSize(                       // Add this code
                preferredSize: Size.fromHeight(60.0),      // Add this code
                child: Text(''),                           // Add this code
              ),                                           // Add this code
              flexibleSpace: Container(
                padding: EdgeInsets.all(10),
                height: 340,
                width: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      height: 40,
                    ),
                    Container(
                      height: 60,
                    ),
                    Expanded(child: Container()),
                    Text('TEST'),
                  ],
                ),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: NetworkImage('https://picsum.photos/400/400'),
                        fit: BoxFit.cover)),
              ),
            )
like image 36
anmol.majhail Avatar answered Nov 01 '22 18:11

anmol.majhail