Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a method of state class using its stateful widget?

Tags:

flutter

dart

I have a method in state class, but I need to access that method in outside using its widget class reference,

class TestFormState extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _testState();
  }
}

class _testFormState extends State<TestFormState> {
  int count = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Container(
        color: Colors.green,
            child: Text("Count : $count"),
        ),
    );
  }

  clickIncrease(){
    setState(() { count += 1; });
  }
}

and I need to access the above widget`s clickIncrease in another widget, like below code,

class TutorialHome extends StatelessWidget {

    TestFormState test;

  @override
  Widget build(BuildContext context) {
    // Scaffold is a layout for the major Material Components.
    return Scaffold(
      body: Column(
         children: <Widget>[
            test = TestFormState(),
            FlatButton(
               child: Text("Increase"),
               onPressed: (){
                  test.state.clickIncrease(); // This kind of thing I need to do
               },
            ),
         ]
      ),
    );
  }

I wrote above code just for demostrate the issue.

like image 814
Sachintha Udara Avatar asked May 29 '19 11:05

Sachintha Udara


People also ask

How do you call a method in stateful widget Flutter?

Calling a method of child widget from a parent widget is discouraged in Flutter. Instead, Flutter encourages you to pass down the state of a child as constructor parameters. Hence instead of calling a method of the child, you just call setState in the parent widget to update its children.

How do you use a stateful widget?

To create a stateful widget in a flutter, use the createState() method. The stateful widget is the widget that describes part of a user interface by building a constellation of other widgets that represent a user interface more concretely. A stateful Widget means a widget that has a mutable state.

What is state in stateful widget?

State is information that (1) can be read synchronously when the widget is built and (2) might change during the lifetime of the widget. It is the responsibility of the widget implementer to ensure that the State is promptly notified when such state changes, using State.


1 Answers

I have a trick, but I don't know if it is a bad practice or not.

class TestFormState extends StatefulWidget {

  _TestFormState _testFormState;

  @override
  State<StatefulWidget> createState() {
    _testFormState = _TestFormState();
    return _testFormState;
  }
}

class _TestFormState extends State<TestFormState> {
  int count = 1;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.green,
        child: Text("Count : $count"),
      ),
    );
  }

  clickIncrease(){
    setState(() { count += 1; });
  }
}

Now, you can access it here :

class TutorialHome extends StatelessWidget {

  TestFormState test;

  @override
  Widget build(BuildContext context) {
    // Scaffold is a layout for the major Material Components.
    return Scaffold(
      body: Column(
          children: <Widget>[
            TextButton(
              child: Text("Increase"),
              onPressed: () {
                test._testFormState
                    .clickIncrease(); // This is accessable
              },
            ),
          ]
      ),
    );
  }
}

I suggest taking a look at ValueNotifier

like image 119
Ismaeel Sherif Avatar answered Oct 21 '22 12:10

Ismaeel Sherif