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
AppBar has a fixed height of 56.
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.
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.
toolbarHeight. Defines the height of the toolbar component of an AppBar.
Simply use collapsedHeight
and specify your minimum height.
SliverAppBar(
// Display a placeholder widget to visualize the shrinking size.
flexibleSpace: Placeholder(),
expandedHeight: 300,
collapsedHeight: 60,
);
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.
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)),
),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With