I have a parent widget called (for this snippet) MainLayout, and a child widget called ChildWidget. I want to add an IconButton in the actions of the AppBar of the parent widget in the child after executed its build method. The actions shown in the AppBar of the parent are stored in its state.
I have a problem with the approach I'm using to do that: The callback triggered after the build method of child widget to set the state of its parent widget doesn't trigger the build method of its parent, but the state (of the parent) has changed. As a result, the search icon is not mounted in the actions of the AppBar of the MainLayout.
Why is working on that way and how to solve it?
The code below is an example of how my child and parent widget looks like:
child_widget.dart:
class ChildWidget extends StatelessWidget {
// Add a search button in the AppBar of MainLayout widget.
void _updateAppBar(BuildContext context) async {
InheritedMainLayout.of(context).appBar.updateActions(<Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () => showSearch(
context: context,
delegate: SearchPageDelegate(),
),
),
]);
}
@override
Widget build(BuildContext context) {
_updateAppBar(context);
return Container(
width: 100,
height: 100,
color: Colors.blue,
);
}
}
parent_widget.dart:
class ParentWidget extends StatefulWidget {
@override
_ParentWidgetState createState() => new _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
// This is the method that is passed as a parameter to the ChildWidget to update the actions of the AppBar.
void _updateAppBarActions(List<Widget> actions) =>
setState(() => _appBarActions = actions);
@override
Widget build(BuildContext context) {
return InheritedMainLayout( // The inherited widget used to use the _updateAppBarActions method in the child widget
appBar: new AppBarConfig(
actions: _appBarActions,
),
child: Scaffold(
appBar: AppBar(
title: Text("Title of the App"),
actions: _appBarActions, // Actions showed in the AppBar are from the state, _appBarActions
),
body: Padding(
padding: EdgeInsets.all(20.0),
child: _currentChild, // Here is loaded the builded widget called ChildWidget.
),
drawer: AppDrawer(
items: _drawerItems,
onTap: _onChangePage,
),
),
);
}
}