为什么这给我一个错误?我该如何修复它(上下文错误)

时间:2021-05-05 10:04:42

标签: flutter

我在其他地方使用了此代码行,但没有出现错误

AutoSizeText(
        "عنوان"
        ,maxLines: 1
        ,overflow: TextOverflow.ellipsis,
        minFontSize: 10,
        style:GoogleFonts.rubik(fontSize: 25,color: Theme.of(context).indicatorColor,fontStyle: FontStyle.normal,fontWeight: FontWeight.w500),
      ),

Picture 1 enter image description here

1 个答案:

答案 0 :(得分:1)

您在状态类 generateItem() 之外提取了小部件 _picturesState

因此您必须将该状态类的 context 作为 generateItem() 的参数传递。然后您就可以在 Theme.of(context) 内使用该上下文。

您的最终解决方案是:

Card generateItem(BuildContext context) {
  return Card(
    //..
  );
}

当你调用这个小部件时,只需将上下文作为参数传递,例如:

return Container(
  child: Column(
    children:[
      generateItem(context);
    ]
  )
);

这应该在你的状态类中,它有自己的上下文。