Home > database >  How to receive one variable from another widget in flutter?
How to receive one variable from another widget in flutter?

Time:02-10

*this is statefulWidget 1

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

  @override
  _DiceRollState createState() => _DiceRollState();
}

class _DiceRollState extends State<DiceRoll> {
  
  int randomTime = 1;

  update(BuildContext context) {
    setState(() {
     
      randomTime = Random().nextInt(120)   1;
    });
  }

 @override
  Widget build(BuildContext context) {
    return Container( 

Text(randomTime),
      
    );
  }

*this is statefulWidget 2

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

  @override
  _StatedemoTwo createState() => _StatedemoTwo();
}

class _StatedemoTwo extends State<demoTwo> {

int timerSet = time;
// I want to receive the random time from the first statefulWidget widget  here.. How to get the time here.


Timer? timer;

startTimer(BuildContext context) {
    timer = Timer.periodic(
      const Duration(seconds: 1),
      (_) {
        if (timerSet > 0) {
          if (mounted) {
            setState(() {
              timerSet--;
            });
          }
        } else {
          stopTimer();
          goBackHome();
        }
      },
    );
  }
  

  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}




CodePudding user response:

Just copy the randomTime variable from widget 1 to widget 2.

Widget 1

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

  @override
  _DiceRollState createState() => _DiceRollState();
}

class _DiceRollState extends State<DiceRoll> {
  
  int randomTime = 1;

  update(BuildContext context) {
    setState(() {
     
      randomTime = Random().nextInt(120)   1;
    });
  }

 @override
  Widget build(BuildContext context) {
    return Column(
        children: <Widget>[ 
        Text(randomTime),
        //copy with constructor
        demoTwo(time: randomTime),
       ],
    );
  }

Widget 2

class demoTwo extends StatefulWidget {
  int? time;

  demoTwo({this.time});    

  @override
  _StatedemoTwo createState() => _StatedemoTwo();
}

class _StatedemoTwo extends State<demoTwo> {

int timerSet = widget.time;
// I want to receive the random time from the first statefulWidget widget  here.. How to get the time here.


Timer? timer;

startTimer(BuildContext context) {
    timer = Timer.periodic(
      const Duration(seconds: 1),
      (_) {
        if (timerSet > 0) {
          if (mounted) {
            setState(() {
              timerSet--;
            });
          }
        } else {
          stopTimer();
          goBackHome();
        }
      },
    );
  }
  

  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

Alternatively, use the Provider package to update and manage state. https://pub.dev/packages/provider

CodePudding user response:

there is the solution: https://stackoverflow.com/a/71036890/16241837.

that's also from me

  • Related