错误:元素类型“TextSpan”无法分配给列表类型“Widget” - Flutter

bil*_*_bo 1 mobile android dart 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,
                  ),
                ),
),
                //...
Run Code Online (Sandbox Code Playgroud)

Fad*_*uad 5

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,
            ),
          ),
        )
Run Code Online (Sandbox Code Playgroud)

这会工作得很好;你的完整代码:

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,
                ),
              ),
            ),
          ],
        ),
Run Code Online (Sandbox Code Playgroud)