I'm trying to implement a sliver app bar in my flutter app but it just won't respond at all
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: FlexibleSpaceBar(
title: Text("title"),
),
),
],
),
));
}
}
You need to add more slivers on CustomScrollView, when the viewport is bigger than slivers height, you will get the effect. Try adding SliverList or other sliver,
body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: FlexibleSpaceBar(
title: Text("title"),
),
),
SliverToBoxAdapter(
child: SizedBox(
height: 3333,
),
)
],
),
You can check this video
You can change it to this.
Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: CustomScrollView(
slivers: [
SliverAppBar(
expandedHeight: 200,
flexibleSpace: FlexibleSpaceBar(
title: Text("title"),
),
expanded: true,
),
],
),
));
}}
Related
I am using CustomScrollView inside Scaffold ,dont't know why it's not detecting Scrolling behaviour.
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
// SliverAppBar(floating: true,),
SliverAppBar(
expandedHeight: 300,
flexibleSpace: FlexibleSpaceBar(
background: Image.asset(
"assets/image/food.png",
width: double.maxFinite,
fit: BoxFit.cover,
),
),
)
],
),
);
}
}
try using SingleChildScrollView()
Im so confuse why my sliverappbar doesnt stretch and zoom when I reach the top list. I following the flutter video
https://youtu.be/mSc7qFzxHDw
I tried the following code
class AppBar5 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: Text("title"),
expandedHeight: 200,
stretch: true,
flexibleSpace: FlexibleSpaceBar(
background: Container(
width: MediaQuery.of(context).size.width,
height: 200,
child: Image.asset("assets/images/hot.jpg", fit: BoxFit.cover)
),
)
),
SliverList(
delegate: SliverChildBuilderDelegate(
(_, index) => ListTile(
title: Text("Index: $index"),
),
),
)
],
),
);
}
}
Add Bouncing ScrollPhysics to CustomScrollView
CustomScrollView(
physics: BouncingScrollPhysics(),
...
),
I am newbie and I am still trying to get Widgets and wrapping concept. For me it is probably hardest part of to learn Flutter.
I have Sliver AppBar and I have to get fetch some data and using FutureBuilder. Here is code
#override
Widget build(BuildContext context) => Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
backgroundColor: Colors.red,
expandedHeight: 200,
floating: true,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
background: Image.network(
'https://source.unsplash.com/random?monochromatic+dark',
fit: BoxFit.cover,
),
title: Text('Flexible Title'),
centerTitle: true,
),
//title: Text('My App Bar'),
leading: Icon(Icons.arrow_back),
actions: [
Icon(Icons.settings),
SizedBox(width: 12),
],
),
],
),
);
}
so my problem is how can I integrate my FutureBuilder snippet to this code.
Here is FutureBuilder:
FutureBuilder<Article>(
future: _futureArticle,
builder: (BuildContext context, AsyncSnapshot<Article> snapshot) {
if (snapshot.hasData) {
final article = snapshot.data?.data;
any help
The correct wrapping:
Widget build(BuildContext context) => Scaffold(
body: FutureBuilder<Article>(
future: _futureArticle,
builder: (BuildContext context, AsyncSnapshot<Article> snapshot) {
if (snapshot.hasData) {
final article = snapshot.data?.data;
return CustomScrollView(
slivers: [
SliverAppBar(
// backgroundColor: Colors.red,
expandedHeight: 200,
floating: true,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
background: Image.network(article == null
? 'https://www.tonymacx86.com/attachments/2019-02-05-07-50-43-jpeg.385401/'
: article.imageUrl!),
title: Text('${article!.title}'),
centerTitle: true,
),
leading: const Icon(Icons.arrow_back),
actions: const [
Icon(Icons.settings),
SizedBox(width: 12),
],
),
],
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
I'm creating an app where I need to implement WhatsApp like AppBar i.e to hide app bar on scroll down and show on reverse.
You can use the SliverAppBar and CustomScrollView to achieve this effect. A sample implementation:
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: false,
snap: false,
floating: false,
expandedHeight: 160.0,
flexibleSpace: const FlexibleSpaceBar(
title: Text('AppBar'),
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: index.isOdd ? Colors.white : Colors.black12,
height: 100.0,
child: Center(
child: Text('$index', textScaleFactor: 5),
),
);
},
childCount: 50,
),
),
],
),
);
}
When i scoll, I wish could scroll SliverAppBar first.
How can i do it? now sub_page first.
The expected effect:
In a scroll action, the preference is SliverAppBar. After the SliverAppBar is displayed/hidden, continue scrolling the sub_page. demo(https://github.com/fanybook/cornerstone/blob/master/_docs/flutter_improve_scroll_priority.mp4?raw=true)
The point is there are subpages(and BottomNavigationBar). if single page can be implemented via multiple SliverAppBar/bottom and NestedScrollView's body/SliverList.
A Minimal E.g of what You are looking for -
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
title: Text('Demo'),
pinned: false,
bottom: PreferredSize(
child: TabBar(
tabs: <Widget>[
Text('Tab 1'),
Text('Tab 2'),
Text('Tab 3'),
],
),
preferredSize: Size.fromHeight(25.0)),
),
SliverList(
delegate: SliverChildBuilderDelegate((context, int) {
return Text('Dummy text');
}),
),
],
),
),
);
}