Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

widget - Flutter: Calling SetState() from another class

I am trying to make a simple image that appears or disappears when a button is pushed. This button resides in a separate class to the image, so in Flutter this creates a massive headache of an issue.

I have read many forums on this and I have tried all the solutions posed but none of them are working for me.

What I am trying to do:

class SinglePlayerMode extends StatefulWidget {
  @override
  SinglePlayerModeParentState createState() => SinglePlayerModeParentState();
}

class SinglePlayerModeParentState extends State<SinglePlayerMode> {
  bool coinVisible = false;

  toggleCoin() {
    setState(() {
      coinVisible = !coinVisible;
    });
  }

Widget topMenuRow() {
  return Stack(
    children: [
      Column(
        children: [
          coinVisible == true ?
          Padding(
            padding: EdgeInsets.all(50),
            child: Container(
              height: 60,
              width: 60,
              color: Colors.blueGrey[0],
              decoration: BoxDecoration(
                color: Colors.blueAccent,
                image: DecorationImage(
                  image: ExactAssetImage('lib/images/coin_head.jpg'),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ) : Container(
            height: 60,
            width: 60,
            color: Colors.black,
          ),
        ],
      ),
    ],
  );
 }

@override
Widget build(BuildContext context) {
  return Scaffold(
      child: ListView(
        padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
        children: [
          topMenuRow(),
          SizedBox(height: 40),
        ],
      ),
    ),
  );
}

And this is the separate class which I would like to trigger the SetState() on coinVisible from:

class dropDownMenu extends StatefulWidget {  @override
  _dropDownMenuState createState() => _dropDownMenuState();
}

class _dropDownMenuState extends State<dropDownMenu> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget> [
        Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Container(
              child: Opacity(
                opacity: 0.0,
                child: FloatingActionButton(
                  heroTag: null,
                  onPressed:  (){
                    //SOMEHOW CALL SetState() ON coinVisble HERE!
                  },
                ),
              ),
          );
       }
  }

But nothing I have tried is working, and I have lost hours.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It simple, you need to send your SinglePlayMode::toggleCoin function as callback to dropDownMenu class.

class dropDownMenu extends StatefulWidget {  
        final _callback; // callback reference holder
                                   //you will pass the callback here in constructor
    dropDownMenu( {@required void toggleCoinCallback() } ) :
       _callback = toggleCoinCallback;
        @override
      _dropDownMenuState createState() => _dropDownMenuState();
    }

    class _dropDownMenuState extends State<dropDownMenu> {
      @override
      Widget build(BuildContext context) {
        return Stack(
          children: <Widget> [
            Column(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                Container(
                  child: Opacity(
                    opacity: 0.0,
                    child: FloatingActionButton(
                      heroTag: null,
                      onPressed:  (){
                        widget?._callback(); // callback calling
                      },
                    ),
                  ),
              );
           }
      }

Then when you create a dropDownMenu class instance in your SinglePlayerMode class you will do

    dropDownMenu(
       toggleCoinCallback: toogleCoin,
    );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...