Skip to main content

How To Change Value In Appbar Text Flutter On Button Click From Other class

 

How to Update State of Parent StatefulWidget from other Child StatefulWidget in Flutter?


Hi! all today we going to learn how we can update or change text of parent StatefulWidget in appbar from the child StatefulWidget in flutter.
Same as we can also change any color, icon or other data of parent widget.



class ParentWidget extends StatelessWidget {
  int cartitem = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
titleSpacing: 0,
leading: Icon(
Icons.location_on,
size: 20,
color: Colors.white,
),
title: Text("Title"),
actions: <Widget>[
Stack(
children: [
IconButton(
icon: Icon(Icons.shopping_cart),
color: Colors.white,
),
Positioned.directional(
textDirection: Directionality.of(context),
top: 8,
end: 12,
child: CircleAvatar(
backgroundColor: Colors.red,
radius: 5.5,
child: Center(
child: Text(
cartitem.toString(),
style: Theme.of(context).textTheme.bodyText2!.copyWith(
color: Theme.of(context).scaffoldBackgroundColor,
fontSize: 9),
)),
),
)
],
),
],
),
body: ChildWidget(function: refreshCart)
);
}

refreshCart() {
setState(() {
cartitem++;
print("cart item updated : " + cartitem.toString());
});
}
}

class ChildWidget extends StatefulWidget {
final VoidCallback function;

ChildWidget({Key? key, required this.function}) : super(key: key);

@override
ChildWidgetState createState() => ChildWidgetState();
}

class ChildWidgetState extends State<ChildWidget> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.teal,
width: double.infinity,
alignment: Alignment.center,
child: ElevatedButton(
child: Text("Call method in parent"),
onPressed: () => widget.function(), // calls method in parent
),
);
}
}

Given code will update count of cart in parent widget from child widget on button click.
Output :




The above written code is working fine hope this work for you also in your concept.
Thankyou for visiting here.

Comments

Popular posts from this blog

Fix Blank Screen issue on app start by Splash Screen

  Flutter - Splash Screen for Android and iOS What is Splash Screen? Splash Screen (or Launch Screen on iOS) is the very first page of your app seen by users. So it would be great to make a good first impression. I would like to present you a few ways to create a splash screen in your Flutter application using native solutions or Flutter package. Our goal is to create splash screens as shown below (from left: iOS 15, Android 12, Android 8): iOS Launch Screen using Storyboard We are able to set a splash screen for iOS in two ways. The first one is by using Storyboard. 1.      First of all, we need to open our project in Xcode. Being in the main directory of the project, we can do that using  open ios/Runner.xcworkspace   command. 2.      Then, we need to add AssetImage, which is going to be our icon on the splash screen. As you can see in the image below, we need to provide 3 different resolutions of an icon: 1x, 2x and 3x (more details  ...