Reputation: 53
I have a network call from child stateful widget. For that, I need to show CircularProgressIndicator in parent widget containing the Scaffold. Is there any way to show it?
Parent Widget:
class OrderDetailPage extends StatefulWidget {
@override
_OrderDetailPageState createState() => _OrderDetailPageState();
}
class _OrderDetailPageState extends State<OrderDetailPage> {
bool isloading = false;
Widget build(BuildContext context){
return Scaffold(body: isLoading ? Center(child: CircularProgressIndicator() : Container(
child: ChildWidget()
));
}
}
Child:
class ChildWidget extends StatefulWidget {
@override
ChildWidgetState createState() => ChildWidgetState();
}
class ChildWidgetState extends State<OrderDetailPage> {
return Center(child: RaisedButton(onPressed: () { // need to make isloading in parent widget to true and false})
}
Upvotes: 0
Views: 199
Reputation: 3074
There are two ways of passing State
up into the Widget Tree.
Passing a callback function from parent to child:
class OrderDetailPage extends StatefulWidget {
@override
_OrderDetailPageState createState() => _OrderDetailPageState();
}
class _OrderDetailPageState extends State<OrderDetailPage> {
bool isloading = false;
Widget build(BuildContext context){
return Scaffold(body: isLoading ? Center(child: CircularProgressIndicator() : Container(
child: ChildWidget(callback: () => setState(() => isLoading = !isLoading))
));
}
}
Child:
class ChildWidget extends StatefulWidget {
ChildWidget({this.callback});
@override
ChildWidgetState createState() => ChildWidgetState();
}
class ChildWidgetState extends State<OrderDetailPage> {
return Center(child: RaisedButton(onPressed: () {
widget.callback();
}
The late alternative is to use a State Management Solution like InheritedWidget
, the Provider
Package or one of many others out there.
Upvotes: 1