首页 > 解决方案 > 为什么这会给我一个错误?以及如何修复它(上下文错误)

问题描述

我在其他地方使用了这个代码行,但它没有给出错误

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

图片1 在此处输入图像描述

标签: flutter

解决方案


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

因此,您必须context将该状态类的作为参数传递给generateItem(). 然后您可以在Theme.of(context).

您的最终解决方案是:

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

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

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

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


推荐阅读