我只是想制作一个向用户提供随机文本的应用程序。
但我很困惑,我无法访问我的变量。我无法将随机数附加到 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
混合使用?
答案 0 :(得分:1)
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),))
答案 1 :(得分:0)
您可以将文本放入 List
,并使用 numm
获取值:
List texts = [text1,text2,text3,text4,text5,text6,text7,text8];
var numm = Random().nextInt(texts.length);
Text(texts[numm],style: TextStyle(color: Colors.white))
答案 2 :(得分:0)
试试这个
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.';
List<String> list = [text1, text2, text3, text4, text5, text6, text7, text8];
final rand = Random().nextInt(7);
Text(list[rand], style: TextStyle(color: Colors.white),);