How do I generate a random six (6) digit number in flutter on a button click to display on the next page?
Container(
width: MediaQuery.of(context).size.width * 3.5/4,
height: MediaQuery.of(context).size.height * 0.35/4,
child: RaisedButton(
color: Colors.indigoAccent,
textColor: cc.WHITE,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
onPressed: () {
push(context, Invite());
},
child: new Text(
'GENERATE CODE',
style: new TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
//text: 'GENERATE CODE',
),
),
You can use the Random class
Container(
width: MediaQuery.of(context).size.width * 3.5/4,
height: MediaQuery.of(context).size.height * 0.35/4,
child: RaisedButton(
color: Colors.indigoAccent,
textColor: cc.WHITE,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
onPressed: () {
int min = 100000; //min and max values act as your 6 digit range
int max = 999999;
var randomizer = new Random();
var rNum = min + randomizer.nextInt(max - min);
push(context, Invite(randomNum: rNum)); //pass your random number through
},
child: new Text(
'GENERATE CODE',
style: new TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
//text: 'GENERATE CODE',
),
),
In your class
class Invite extends StatefulWidget{
Invite(Key key, this.randomNumber):super(key:key);
final int randomNumber;
....//your code
//to retrieve the number that has been sent use widget.randomNumber
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments