This is a sample code to demonstrate the unexpected behaviour.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _bodies = [
Feeds(),
Search(),
];
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: _bodies,
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Feeds'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
],
onTap: (index) => setState(
() => _currentIndex = index,
),
),
);
}
}
class Feeds extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PrimaryScrollController(
controller: ScrollController(),
child: Scaffold(
body: NestedScrollView(
controller: PrimaryScrollController.of(context),
headerSliverBuilder: (_, __) {
return [
SliverAppBar(
pinned: true,
title: Text('Feeds'),
),
];
},
body: ListView.builder(
itemBuilder: (_, int index) {
return Container(
height: 100,
margin: EdgeInsets.only(
left: 16,
right: 16,
bottom: 16,
),
decoration: BoxDecoration(
color: Colors.blueGrey.withOpacity(0.3),
borderRadius: BorderRadius.circular(15),
),
);
},
),
),
),
);
}
}
class Search extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PrimaryScrollController(
controller: ScrollController(),
child: Scaffold(
body: NestedScrollView(
controller: PrimaryScrollController.of(context),
headerSliverBuilder: (_, __) {
return [
SliverAppBar(
pinned: true,
title: Text('Search'),
),
];
},
body: ListView.builder(
itemBuilder: (_, int index) {
return Container(
height: 100,
margin: EdgeInsets.only(
left: 16,
right: 16,
bottom: 16,
),
decoration: BoxDecoration(
color: Colors.lightGreen.withOpacity(0.5),
borderRadius: BorderRadius.circular(15),
),
);
},
),
),
),
);
}
}