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
178 views
in Technique[技术] by (71.8m points)

dart - Flutter, calling a function inside the button

I am new to flutter and I make some practices. I have a StatelessWidget called doChange and makeChange and one StatefulWidget. This class which is statefulwidget I made child of the home page of the app also. But, I think that it is unnecessary to define here. My purpose in this case is that, I want to change the state of the button make open,make closed and at the same time the text open and close will also change. I think that class changeText has not problem but in makeChange class I have some trouble with creating constructor and function to call into onPress. The states do not change. How can i solve this or is that any way to do this without function ?

class changeText extends StatelessWidget{

  final doChange;  
  changeText({@required this.doChange}); 

   @override
   Widget build(BuildContext context){
     return Container(
        //some codes
        //some codes
        child: doChange ? Text("open") : Text("close"),
     );
   }
}
class makeChange extends StatelessWidget{ 
     
     final changeState;
     makeChange({@required this.changeState}); // I want to add constructor here lets say onPressButton 

     whenPressed(){ // I want to create a function with the that constructor that I have add.
          
     }
     @override
     Widget build(BuildContext context){
       return Container(
            //some codes
            //
           child: Column(
              children: [
                MaterialButton(
                  //some codes
                  //
                  onPressed: () {} // Here I want to call a function when press the button. 
                  child: changeState ? Text("make open") : Text("make close"),
                ),
              ],
            ),
       );
     }
} 
class Mainarea extends StatefulWidget{
   @override
   _MainareaState createState() => _mainAreaState();
}

class _MainareaState extends State<Mainarea> {

   bool isChange= false; 

   @override
   Widget build(BuildContext context){ 
       return Container(
          //some codes
          // 
          child: Column(
            children: <Widget>[ 
                changeText(doChange: !this.isChange),
                makeChange(changeState: !this.isChange), 
            ],
          ),
       );
    }
}
    
question from:https://stackoverflow.com/questions/65626749/flutter-calling-a-function-inside-the-button

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

1 Reply

0 votes
by (71.8m points)

I just added a final Function(bool) callback as a parameter, which can be called inside from the stateless widget, and returns to the calling function. From there you can call setState

class changeText extends StatelessWidget {
  final bool doChange;
  changeText({@required this.doChange});

  @override
  Widget build(BuildContext context) {
    return Container(
      //some codes
      //some codes
      child: doChange ? Text("open") : Text("close"),
    );
  }
}

class makeChange extends StatelessWidget {
  final bool changeState;
  final Function(bool) callback;

  makeChange(
      {@required
          this.changeState,
      @required
          this.callback}); // You can rename this to onPressed or whatever


  @override
  Widget build(BuildContext context) {
    return Container(
      //some codes
      //
      child: Column(
        children: [
          MaterialButton(
            //some codes
            //
            onPressed: () => callback( changeState),
            child: changeState ? Text("make close") : Text("make open"), //I had to swap these around to make the text correct
          ),
        ],
      ),
    );
  }
}

class Mainarea extends StatefulWidget {
  @override
  _MainareaState createState() => _MainareaState();
}

class _MainareaState extends State<Mainarea> {
  bool isChange = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      //some codes
      //
      child: Column(
        children: <Widget>[
          changeText(doChange: !this.isChange),
          makeChange(
            changeState: !this.isChange,
            callback: (bool val) {
              setState(() => isChange = val); //this is your function that returns and resetst the value in the parent widget
            },
          ),
        ],
      ),
    );
  }
}

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

...