我试着保持代码的整洁,但是突然间它就不工作了,没有错误,但是尽管我点击了ElevatedButton
,什么也没发生。
我猜onPressedGenerator
中的变量存在兼容性问题。
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:randome/HomeScreen/setting_screen.dart';
import 'package:randome/constant/color.dart';
class homescreen extends StatefulWidget {
const homescreen({Key? key}) : super(key: key);
@override
State<homescreen> createState() => _homescreenState();
}
class _homescreenState extends State<homescreen> {
final rand = Random();
Set<int> number = {};
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: PRIMARY_COLOR,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
header(),
middle(number: number),
bottom(onPressed: onPressedGenerater),
],
),
),
),
);
}
onPressedGenerater() {
final rand = Random();
Set<int> number = {};
setState(() {
for (int i = 0; i < 4; i++) {
number.add(rand.nextInt(1500));
print(number) ;
}
if (number.length > 4 || number.length <3) {
number.clear();
for (int i = 0; i < 4; i++) {
number.add(rand.nextInt(1500));
}
}
});
}
}
class header extends StatelessWidget {
const header({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Text(
"랜덤숫자 생성기",
style: TextStyle(
color: Colors.white, fontSize: 30.0, fontWeight: FontWeight.w700),
),
IconButton(
onPressed: () async {
final result = await Navigator.of(context)
.push(MaterialPageRoute(builder: (BuildContext context) {
return settingscreen();
}));
},
icon: Icon(
Icons.settings,
color: RED_COLOR,
)),
]);
}
}
class middle extends StatelessWidget {
Set<int> number;
middle({required this.number, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: number
.toList()
.asMap()
.entries
.map(
(x) => Padding(
padding: EdgeInsets.only(bottom: x.key == 2 ? 0 : 16.0),
child: Row(
children: x.value
.toString()
.split("")
.map(
(y) => Image.asset(
"asset/img/$y.png",
height: 70.0,
width: 50.0,
),
)
.toList(),
),
),
)
.toList(),
),
);
}
}
class bottom extends StatelessWidget {
final VoidCallback onPressed;
const bottom({required this.onPressed, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: RED_COLOR,
),
onPressed: onPressed,
child: Text("생성하기")),
);
}
}
我测试了它是否有效。
通过在onPressedGenerator
函数中添加代码print(number)
。
见下文:
x1c 0d1x它仍然工作。所以我认为onPressedGenerator
和class _homescreenState
之间的变量存在兼容性问题。
我该怎么解决呢?
世界上最好的人请帮帮我!
1条答案
按热度按时间gj3fmq9x1#