如何在单击按钮时随机生成一个六 (6) 位数字以显示在下一页上?
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',
),
),
最佳答案
您可以使用 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',
),
),
在你的类里面
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
}
关于flutter - DART:如何在单击按钮时随机生成一个六 (6) 位数字以显示在下一页上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57152760/