我创建了这个应用程序,其中该应用程序会生成一个随机符号列表,但是每个第5个元素都有相同的符号,并且该符号随后会最终打印出来。一切正常,直到我到达最后一页,我想创建一个按钮来刷新整个应用程序,并且符号再次变得随机。我尝试使用该按钮并将其链接以再次转到首页,但是图标保持不变。
这是我的代码,如果我很杂乱,我很抱歉:- https://dartpad.dev/725f4a6f4bd97c5a4a9e43b2af9159d6
import 'package:flutter/material.dart';
import "dart:math";
var list = ['❤', '✌', '?', '?', '?', '?', '❄', '?'];
List<dynamic> notes = new List();
final _random2 = new Random();
var element2 = list[_random2.nextInt(list.length)];
var x = element2;
void main() {
runApp(MyApp()
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int index = 0;
List<dynamic> myText = [
'All symbols are random',
'but all number divisible by 5 have same symbols',
'last page will show you the common symbol',
];
void changeText() {
setState(() {
index++;
if (index == myText.length) {
index = 0;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}
for (int i = 0; i < 100; i++) {
if ((i + 1) % 5 == 0) {
notes.add('${i + 1}.) $x');
} else {
final _random = new Random();
var element = list[_random.nextInt(list.length)];
notes.add('${i + 1}.) $element');
}
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('random'),
),
body: SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(50),
child: Text(
myText[index],
),
),
],
),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.navigate_next),
onPressed: changeText,
),
);
}
}
class SecondRoute extends StatefulWidget {
@override
_SecondRouteState createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("random"),
),
body: Container(
color: Colors.white10,
padding: EdgeInsets.all(16.0),
child: HomePage(notes),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.navigate_next),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute2()),
),
));
}
}
class HomePage extends StatefulWidget {
final List<dynamic> notes;
HomePage(this.notes);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: widget.notes.length,
itemBuilder: (context, pos) {
return Padding(
padding: EdgeInsets.only(bottom: 16.0),
child: Card(
color: Colors.white,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 24.0, horizontal: 16.0),
child: Text(
widget.notes[pos],
style: TextStyle(
fontSize: 18.0,
height: 1.6,
),
),
),
),
);
},
);
}
}
class SecondRoute2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("List"),
),
body: Container(
color: Colors.white10,
padding: EdgeInsets.all(16.0),
child: Text('$x'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.refresh),
onPressed: () => MyApp));
}
}
答案 0 :(得分:0)
您重复的元素是这个,在文件作用域设置:
var element2 = list[_random2.nextInt(list.length)];
在您的changeText
函数中,只需像这样重新分配它:
void changeText() {
setState(() {
// not needed after all! final _random = new Random(); // add this
element2 = list[_random2.nextInt(list.length)]; // ... and this
x = element2; // and this
index++;
if (index == myText.length) {
index = 0;
notes=[]; // update: add this
您不必重新启动应用程序-只需重新分配即可。注意:您有element2
和x
似乎总是具有相同的值。您可能应该只选择一个变量并使用它,而不要让两个具有相同的值。