flutter - DART:如何在单击按钮时随机生成一个六 (6) 位数字以显示在下一页上?

标签 flutter dart

如何在单击按钮时随机生成一个六 (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/

相关文章:

flutter - 更改一个 Provider 中的属性会将另一个 Provider 中的属性更改为 List Flutter

flutter - 如何处理不需要的小部件构建?

flutter - 如何重置我的重置此应用程序的当前状态

image - 如何在我的 flutter 应用程序中访问特定目录中的文件

angular - 如何在 Angular Dart 中使用 Angular Material 表

android - 无法在 FutureBuilder 中滚动 ListView

flutter - MediaQuery.of()一直显示

swift - Swift 中 AnyClass 的 Dart 表示是什么

Flutter FutureBuilder 并使用 Navigator.pushNamed 导航到不同的路线

flutter - FlutterPermission_Handler IOS位置