首页 > 解决方案 > 在颤振中使用随机变量

问题描述

我只是想制作一个向用户提供随机文本的应用程序。但我很困惑,我无法访问我的变量。我不能将我的随机数附加到text单词上。

这是我的变量:

    var text1 = 'Of cource do it!';
    var text2 = 'Do it.';
    var text3 = 'why don\'t you do it faster?';
    var text4 = 'I have no idea about it.';
    var text5 = 'it\'s better to don\'t do it.';
    var text6 = 'Don\'t do it.';
    var text7 = 'if I was you , I would not do it.';
    var text8 = 'Never do it.';

这是我制作随机数的方法:

    void ran(){
      numm = Random().nextInt(8)+1;
    }

这就是我尝试用text单词附加随机数的方式:

Text('$text$numm',style: TextStyle(color: Colors.white),)),

我怎样才能random number混合text

标签: flutterdart

解决方案


 List<String> text = [
    'Of cource do it!',
    'Do it.',
    'why don\'t you do it faster?',
    'I have no idea about it.',
    'it\'s better to don\'t do it.',
    'Don\'t do it.',
    'if I was you , I would not do it.',
    'Never do it.'];
      
      int ran() {
        numm = Random().nextInt(8) + 1;
        return numm;
      }

然后像这样使用

Text(text[ran()],style: TextStyle(color: Colors.white),))

推荐阅读