I have a parent widget which is a Container()
and a child widget which is a FutureBuilder
...
In my app I need to change the height of the container so that it fits the newly added items in the FutureBuilder
But the problem is when I setState
and change the parent widget's (Container()'s
) height the FutureBuilder
gets rebuilt again
Now, that's to be expected and is the normal behavior...
Now the question. How can I prevent my child's widget from rebuilding and rebuild only the parent widget?
Like a way to save the data into the RAM or something...
Also, I'm using AutomaticKeepAliveClientMixin
but to no avail;
Here is my code
Parent
\\ Somewhere here I call setState and change the value of _latestPostsHeight
Container(
child: LatestPosts(),
height: _latestPostsHeight,
),
And this my LatestPosts()
which is a FutureBuilder
class _LatestPostsState extends State<LatestPosts>
with AutomaticKeepAliveClientMixin {
bool get wantKeepAlive => true;
bool _isFirstTime = true;
Future<List<Post>> _posts() async {
final Future<List<Post>> _posts =
context.read(fetchPostsProvider({'perPage': 5, 'pageNum': 1}).future);
return _posts;
}
@override
Widget build(BuildContext context) {
super.build(context);
return FutureBuilder(
future: _posts(),
builder: (BuildContext context, AsyncSnapshot<List<Post>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Column(
children: [
for (var i = 0; i < 7; i++) PostShimmer(),
],
);
} else if (snapshot.connectionState == ConnectionState.done) {
if (_isFirstTime) {
SchedulerBinding.instance
.addPostFrameCallback((_) => setState(() {
_isFirstTime = false;
final boolProvider =
context.read(latestPostsDataLoaded);
boolProvider.state = true;
}));
}
return Column(
children: [
for (var i = 0; i < snapshot.data.length; i++)
SimplePostContainer(
data: snapshot.data, index: i, type: SearchOrPost.post)
],
);
} else {
return Container();
}
});
}
}
What can I do?