android - 错误:无法将元素类型“TextSpan”分配给列表类型“Widget”-Flutter
问题描述
我想优化我的代码,GridView
因为我有 16 个 TextSpan 来对齐 4x4。问题是GridView 不接受 TextSpans。出现这个错误:The element type 'TextSpan' can't be assigned to the list type 'Widget'
。我已经尝试删除,<Widget>
但它没有工作。
这是代码:
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 4, // 4 tiles horizontally
children: <Widget>[
TextSpan(
text: widget.result + ' ',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: checkdominantA(widget.predominant, widget.result),
height: 2.5,
letterSpacing: 0.7,
),
),
TextSpan(
text: widget.result2 + ' ',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: checkdominantA(widget.predominant, widget.result2),
height: 2.5,
letterSpacing: 0.7,
),
),
),
//...
解决方案
Textspan 不是一个使用 RichText Widget 的小部件:
RichText(
text: TextSpan(
text: widget.result + ' ',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: checkdominantA(widget.predominant, widget.result),
height: 2.5,
letterSpacing: 0.7,
),
),
)
这会很好用;你的完整代码:
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 4,
// 4 tiles horizontally
children: <Widget>[
RichText(
text: TextSpan(
text: widget.result + ' ',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: checkdominantA(widget.predominant, widget.result),
height: 2.5,
letterSpacing: 0.7,
),
),
),
RichText(
text: TextSpan(
text: widget.result2 + ' ',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: checkdominantA(widget.predominant, widget.result2),
height: 2.5,
letterSpacing: 0.7,
),
),
),
],
),
推荐阅读
- javascript - 更改函数内的变量?
- javascript - 原语到包装对象的转换(幕后)
- git - GitHub Web UI 中的“base”和“head”repo 是什么?
- python - Pandas 无法正确将 csv 转换为 hdf5?
- java - Android - 保存图像监听器
- python - 在 python 3 中写入和存储数据的最佳方式是什么
- python - 网页抓取时如何排除标签
- python - Miniconda 安装出错:编辑路径或卸载并重新安装
- c - while(gets_s != NULL) 在 C 编程中没有通过 enter 终止
- .net - 部署应用程序时的最佳实践