Call setState() method of class from a button click of other class in Flutter

122
February 10, 2019, at 6:50 PM

I have a drawer in my main page that changes the body of the scaffold according to selected drawer item, now I need to change the body by tapping on a button in a fragment and call the setState() method of main class with a parameter to update the screen. How can I do this?
this is my Scaffold body:

body: _getDrawerItemWidget(_selectedDrawerIndex));

and when a drawer item is selected the setState() is called and body is filled according to this code :

_getDrawerItemWidget(int pos) {
    switch (pos) {
      case 0:
        return new MainFragment();
      case 1:
        return new CardMgmt();
.........
}
}

but now I need to do this from a button in CardMgmt class.

Answer 1

You can make it with callback function.

In parent Widget you creating child Widget and pass callback as parameter:

...
@override
Widget build(BuildContext context) {
  return LoginPage(onSignedIn: _signedIn);
}
void _signedIn() {
  setState(() {
  });
}

In child widget you call this callback, when you need it:

...    
class LoginPage extends StatefulWidget {
  LoginPage({this.onSignedIn});
  final VoidCallback onSignedIn;
  State<StatefulWidget> createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
...
void callCallbackFunction() {
  widget.onSignedIn();
}
...

Hope it helps. Good Luck!

Answer 2

let me give a super simple approach I use whenever I want to change the state of another class/widget:

define a global variable, then use this variable in the State class of the widget you want to change later. Then you can modify the value of this variable from any method inside your code and state of your first class will change automatically without any direct call of the class' setState() method.

here's an example:

int counter = 0 ;
class _AppState extends State<App> {
var numberOfItems = counter ;
return new MaterialApp(
  title: 'Ads App',
  home: new Text(counter),
 );
}

if you change the value of counter from anywhere from your code the Text widget content will change automatically.

READ ALSO
How to change background color of center element using Snaphelper

How to change background color of center element using Snaphelper

I want to change the background color of the centered position elementI have used vertical snaphelper and my center element is also detected so i just want to add background color to the center element

138
when reaching emulator file system in terminal, taking permisson denied

when reaching emulator file system in terminal, taking permisson denied

I want to look emulator files with adb shell in android stuido terminalI am trying below steps, and it gives me permisson denied

150
Getting the active buttons on the Screen

Getting the active buttons on the Screen

So I'm trying to make a program that scrapes an app for data depending if a certain button is activeThe app doesn't have an API so what I'm left with just reading the active data on the screen

162
Remove leading and trailing whitespaces in JSON keys

Remove leading and trailing whitespaces in JSON keys

I am trying to get a JSON value from my MariaDB server using JSON_EXTRACTHowever, there are some JSON keys having lot of white spaces like end of line, space, tabs etc

111