Home > database >  Fetch state/variable of other Stateful widgets
Fetch state/variable of other Stateful widgets

Time:05-21

I want to fetch the variable of other Stateful Widget

For example, I have three widget such as SmartTag, MySlider, MyHomePage

MyHomePage has SmartTag and MySlider

(this code is not correct just the bulky image)

class _SmartTagState extends State<_SmartTagState>{
    var tagState;

class _MySliderState extends State<_MySliderState>{
    var sliderState;

class MyHomePage extends StatefulWidget {}
class _MyHomePageState extends State<MyHomePage> {
    return Row(
        children:[
        MySlider(),
        SmartTag()
        floatingActionButton: FloatingActionButton(
            onPressed: (){
                print(MySlider.sliderState)//  I want to get the variable fron another widget.
                print(SmartTag.tagState)//  I want to get the variable fron another widget.
            },
            tooltip: 'Increment',
            child: const Icon(Icons.add),
        ), 
    ]
    )
}

What I want to do is, getting the other widget varliables when button is pushed.

So,I have some quesitons.

  1. Is there any way to get the other widgets(SmartTag,MySlider) variable from _MyHomePageState?
  2. _MyHomePageState should have the all variables from the beginning? (However when code is long and comples, it's not useful)

Any help appreciated thank you .

CodePudding user response:

you can pass a callback function to your child widgets and update corresponding variable for that widget inside MyHomePage:

class MyHomePage extends StatefulWidget {
  const MyHomePage({ Key? key }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var tagValue = ...
  var sliderValue = ...
  @override
  Widget build(BuildContext context) {
    return Row(
        children:[
        MySlider(
          onChange: (value){
            setState(() {
              sliderValue = value;
            });
          }
        ),
        SmartTag(
          onChange: (value){
            setState(() {
              tagValue = value;
            });
          }
        )
        floatingActionButton: FloatingActionButton(
            onPressed: (){
                print(MySlider.sliderState)//  I want to get the variable fron another widget.
                print(SmartTag.tagState)//  I want to get the variable fron another widget.
            },
            tooltip: 'Increment',
            child: const Icon(Icons.add),
        ), 
    ]
    );
  }
}

Your child widgets would be something like this:

class SmartTag extends StatefulWidget {
  const SmartTag({ Key? key ,required this.onChange}) : super(key: key);
  final Function(dynamic value) onChange;
  @override
  State<SmartTag> createState() => _SmartTagState();
}

class _SmartTagState extends State<SmartTag> {
  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

CodePudding user response:

Define the variables you want to pull as static and use them like this

class _SmartTagState extends State<_SmartTagState>{
    static tagState;

class _MySliderState extends State<_MySliderState>{
    static sliderState;

class MyHomePage extends StatefulWidget {}
class _MyHomePageState extends State<MyHomePage> {
    return Row(
        children:[
        MySlider(),
        SmartTag()
        floatingActionButton: FloatingActionButton(
            onPressed: (){
                print(MySlider.sliderState)//  I want to get the variable fron another widget.
                print(SmartTag.tagState)//  I want to get the variable fron another widget.
            },
            tooltip: 'Increment',
            child: const Icon(Icons.add),
        ), 
    ]
    )
}
  • Related