我目前正在研究随机辛普森情节发生器。
我希望用户在按下一个按钮后获得随机的季节和随机的情节。 此刻,“按下”启动一个名为“ randomEpisode”的功能。在那里随机选择季节和情节。
现在我不知道如何更新显示季节和情节的文字。
import 'package:flutter/material.dart';
import 'dart:math';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Random random = new Random();
int season = 0;
int episode = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.yellow,
body: SafeArea(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/Loading.jpg'),
fit: BoxFit.fitWidth,
),
),
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(height: 40.0),
Text(
'Season: $season ',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 50.0,
letterSpacing: 2.0,
color: Colors.black,
),
),
SizedBox(height: 10.0),
Text(
'Episode: $episode',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 50.0,
color: Colors.black,
),
),
SizedBox(height: 40.0),
RaisedButton(
onPressed: () => randomEpisode(),
color: Colors.blue,
elevation: 5,
child: const Text('Random',
style: TextStyle(
fontSize: 20,
color: Colors.black,
)),
),
]),
),
),
),
);
}
randomEpisode() {
List<int> numberepisodes = [
13,
22,
24,
22,
22,
25,
25,
25,
25,
23,
22,
21,
22,
22,
22,
21,
22,
22,
20,
21,
23,
22,
22,
22,
22,
22,
22,
22,
21,
23,
22,
];
Random random = new Random();
int season;
int episode;
int maxseason;
int maxepisode;
//_changeEpisode();
print('numberepisodes: $numberepisodes');
maxseason = numberepisodes.length;
print('maxseason $maxseason');
season =
1 + random.nextInt(numberepisodes.length - 1);
print('season $season');
maxepisode = numberepisodes[season - 1];
print('maxepisodes $maxepisode');
episode =
1 + random.nextInt(numberepisodes[season - 1] - 1);
print('Season: $season , Episode: $episode');
}
}
答案 0 :(得分:0)
从randomEpisode()
方法中删除下面的局部变量
int season;
int episode;
然后在randomEpisode()
方法的末尾调用以下方法。
setState((){});