Trigger a function from a widget to a State object

前端 未结 3 1622
情深已故
情深已故 2020-12-05 12:33

This is a simplified version of the scenario:

class ParentWdiegt extends StatelessWidget{
//
//
floatinActionButton: FloatingActionButtonWidget(onPressed:()=         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 12:42

    Try to store the state of the widget and then access

    class MySFWidget extends StatefulWidget {
      SFWidgetState currState;
    
      @override
      SFWidgetState createState(){
        currState = new SFWidgetState();
        return currState;
      };
    
    }
    
    class SFWidgetState extends State {
    
      int prop;
    
      void SomeMethod(){
        // DO Something
      }
    
      @override
      Widget build(BuildContext context) {
        return null;
      }
    
    }
    

    Then, access its property or call method by :

    myWidgetInstance.currState.prop

    myWidgetInstance.currState.SomeMethod()

提交回复
热议问题