Flutter 在颤振应用程序中,当通过加载小部件显示黑屏时,如何停止setState?

Flutter 在颤振应用程序中,当通过加载小部件显示黑屏时,如何停止setState?,flutter,state-management,Flutter,State Management,我试图在我的颤振应用程序中编写一个简单的功能,当用户点击一个对象时,它会改变颜色。我使用以下代码执行此操作: GestureDetector( onTap: () { setState(() { computer = !computer; }); }, child: SubjectCard(subject: computer, index: 4), ), class SubjectCard extends StatelessWidget { var sub

我试图在我的颤振应用程序中编写一个简单的功能,当用户点击一个对象时,它会改变颜色。我使用以下代码执行此操作:

GestureDetector(
  onTap: () {
  setState(() {
   computer = !computer;
    });
  },
  child: SubjectCard(subject: computer, index: 4),
),


class SubjectCard extends StatelessWidget {

  var subject;
  int index;

  SubjectCard({this.subject, this.index});

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(16),
          color: subject == true ? categories[index].color.withOpacity(1) : categories[index].color.withOpacity(0.55),
          // color: pressAttention0 == true ? categories[1].color.withOpacity(1) : categories[1].color.withOpacity(0.55),
        ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  CircleNumber(index: index + 1),
                  Container(
                    width: 60,
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.grey[200],
                      ),
                    ),
                  ),
                ],
              ),
              Expanded(
                child: Container()
              ),
              Text(
                categories[index].name,
                style: kTitleTextCardStyle,
              ),
              SizedBox(height: 2),
              Text(
                '${categories[index].numOfTutors} Tutors',
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              SizedBox(height: 15),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    width: 120,
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.grey[200],
                ),
              ),
            ),
          ],
          ),
        ],
      ),
                         );
  }
}

但是,每当用户单击对象时,颜色会发生变化(这正是我想要的),但整个应用程序会重新加载,用户会立即被发送到带有蓝色加载指示灯的黑屏上。我没有将其编码,也不确定为什么会发生这种情况。屏幕很烦人,我想摆脱它,让应用程序在他们不注意的情况下更改对象的颜色。任何帮助都将不胜感激。

经过一番挖掘,我发现了为什么会发生这种情况,这可能与您遇到的问题不同,但视觉效果是一样的。我不妨在我的场景中描述是什么导致了这个问题,它与在statefulWidget中使用FutureBuilder有关

阅读第二段

未来必须更早获得,例如在State.initState、State.didUpdateConfig或State.didChangeDependencies期间。在构造FutureBuilder时,不能在State.build或stateWidget.build方法调用期间创建它。如果future与FutureBuilder同时创建,则每次重建FutureBuilder的父级时,异步任务都将重新启动

这里的解决方案是将您的未来声明为state类中的一个字段(变量),并在
initState
中设置它,而不是直接在build部分中设置,请参见下面的示例

class MyWidgetState extends State<MyWidget> {
  Future<void> _initializeWidgetFuture;

  @override
  void initState() {
    super.initState();
    _initializeWidgetFuture = MyService.GetSomeData(); // Set the future here!!
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<void>(
      future: _initializeWidgetFuture, // Don't call MyService.GetSomeData() here!!
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(
              child: CircularProgressIndicator(),
            );
          case ConnectionState.active:
          case ConnectionState.done:
            return Text("Component initalized");
          default:
            return null;
        }
      },
    );
  }
}
类MyWidgetState扩展状态{
未来(u)初始化未来;;
@凌驾
void initState(){
super.initState();
_initializeWidgetFuture=MyService.GetSomeData();//在这里设置未来!!
}
@凌驾
小部件构建(构建上下文){
回归未来建设者(
future:_initializeWidgetFuture,//不要在这里调用MyService.GetSomeData()!!
生成器:(上下文,快照){
交换机(快照.连接状态){
案例连接状态。无:
案例连接状态。正在等待:
返回中心(
子对象:CircularProgressIndicator(),
);
案例连接状态.active:
案例连接状态。完成:
返回文本(“组件初始化”);
违约:
返回null;
}
},
);
}
}

您知道为什么会发生这种情况吗?我有完全相同的问题,有时在路由时,有时调用SetState()。
class MyWidgetState extends State<MyWidget> {
  Future<void> _initializeWidgetFuture;

  @override
  void initState() {
    super.initState();
    _initializeWidgetFuture = MyService.GetSomeData(); // Set the future here!!
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<void>(
      future: _initializeWidgetFuture, // Don't call MyService.GetSomeData() here!!
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(
              child: CircularProgressIndicator(),
            );
          case ConnectionState.active:
          case ConnectionState.done:
            return Text("Component initalized");
          default:
            return null;
        }
      },
    );
  }
}