android - 函数中的 setState 不起作用 | 扑
问题描述
我正在执行此_optionSelected
功能(第 63 行),但它不起作用。我尝试更改很多东西,但每次进入小部件页面时,它只会在我的文本中保留索引 0 和白色。它不遵循setState()
功能。图片在这里:
代码在这里:
import 'package:robotics_community_app/utils/card_image_list.dart';
class Components extends StatefulWidget {
const Components({Key key}) : super(key: key);
@override
_ComponentsState createState() => _ComponentsState();
}
class _ComponentsState extends State<Components> {
@override
Widget build(BuildContext context) {
return PageView(
physics: BouncingScrollPhysics(),
children: [
SingleComponent('Fotorresistor', 'assets/images/fotor1.png',
'assets/images/fotor2.png', 'assets/images/fotor3.png', '', ''),
SingleComponent('Potenciómetro', '', '', '', '', ''),
Testing(Colors.blue),
Testing(Colors.green),
],
);
}
}
class Testing extends StatelessWidget {
final Color color;
const Testing(this.color);
@override
Widget build(BuildContext context) {
return Container(
color: this.color,
);
}
}
class SingleComponent extends StatefulWidget {
final String name;
final String image1;
final String image2;
final String image3;
final String description;
final String utilities;
int current_section = 0;
Color info_color = Color(0xffBDE3BE);
Color videos_color = Color(0xffBDE3BE);
Color preguntas_color = Color(0xffBDE3BE);
SingleComponent(this.name, this.image1, this.image2, this.image3,
this.description, this.utilities);
@override
_SingleComponentState createState() => _SingleComponentState();
}
class _SingleComponentState extends State<SingleComponent> {
_optionSelected(int index) {
setState(() {
widget.current_section = index;
widget.videos_color = Color(0xffBDE3BE);
widget.preguntas_color = Color(0xffBDE3BE);
widget.info_color = Color(0xffBDE3BE);
if (index == 0) {
debugPrint("Hola");
// info_color = Color(0xff4AB14E);
widget.info_color = Colors.white;
}
else if (index == 1) {
debugPrint("Hola2");
widget.videos_color = Color(0xff4AB14E);
}else if (index == 2) {
debugPrint("Hola3");
widget.preguntas_color = Color(0xff4AB14E);
}
});
}
@override
Widget build(BuildContext context) {
return
Scaffold(
//backgroundColor: Color(0xff333333),
body: Column(
children: [
Container(
child: Text(
widget.name,
style: TextStyle(
color: Color(0xff4AB14E),
fontSize: 26,
fontWeight: FontWeight.bold),
),
alignment: Alignment.topCenter,
margin: EdgeInsets.only(top: 50),
),
CardImageList(widget.image1, widget.image2, widget.image3),
Padding(
padding: const EdgeInsets.only(left:28.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
FlatButton(onPressed: _optionSelected(0), child: Text('Información', style: TextStyle(fontSize: 17, color: widget.info_color),)),
FlatButton(onPressed: _optionSelected(1), child: Text('Videos', style: TextStyle(fontSize: 17, color: widget.videos_color),)),
FlatButton(onPressed: _optionSelected(2), child: Text('Preguntas', style: TextStyle(fontSize: 17, color: widget.preguntas_color),)),
],
),
),
],
),
);
}
}
如果有人可以帮助我,那就太棒了,谢谢:D
解决方案
您_optionSelected()
以错误的方式调用方法。
而不是这个:onPressed: _optionSelected(0)
像这样调用:onPressed: () => _optionSelected(0)
推荐阅读
- python - 为什么 Pandas get_dummies 函数不执行“枢轴”?
- regex - 用于在版本中添加“build.counter”的文件内容替换器
- css - CSS 人工模拟斜体字体样式
- javascript - 使用 SheetJS 格式化日期
- javascript - 如何避免将相同的项目/产品/行添加到数据表
- command-line - 命令行变量不会在构建脚本上增加
- python - PIL:如何编写文本图像并自动换行?
- android - 为什么 @Named 依赖被忽略?
- pagespeed-insights - 为什么 PageSpeed Insights 将页面速度/字段数据总结为慢?
- mongodb-query - 查询 CosmosDB 时处理此请求时发生未知服务器错误