flutter - 如何在颤振中重新启动整个应用程序?
问题描述
我创建了这个应用程序,该应用程序生成一个随机符号列表,但每个第 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));
}
}
解决方案
您重复的元素是这样的,在文件范围内设置:
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
which 似乎总是具有相同的值。您可能应该只选择一个变量并使用它,而不是使用两个具有相同值的变量。
推荐阅读
- javac - 错误无法找到或加载主类 Twitter4j
- selenium - Webdriver wd= new ChromeDriver();和 ChromeDriver wd= new ChromeDriver();的区别
- mysql - MySQL select 语句返回多个 SUM 和多个 WHERE
- python - 使用 Beautifulsoup 从脚本中提取文本
- opencyc - 如何在 SubL 中检查 (genls Automobile RoadVehicle) 是否为真
- c# - MS Access 获取下一个 UID
- c# - 查找 Bitarray 数组的重复项
- java - 如何为 Hamcrest 匹配器处理此错误
- java - 在 Android Studio 中构建旧的 android 项目,得到 kotlin 错误
- clojure - Compojure - 在路由被命中时进行 HTTP 调用