当我按下返回主屏幕(登录页面)时,加载图标仍在运行



我正在制作一个应用程序来搜索护目镜上的一些项目。

在执行此操作时,搜索应用程序会显示一个加载图标,搜索完成后它会转到第二个屏幕。

如果我按返回进行另一次搜索,加载图标仍在运行。

!isLoading ? FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
onPressed: () async{
String query = getQuery();
List<Recipe> receitas = await getReceitas(query);
Navigator.push(context, MaterialPageRoute(builder: (context)=>result_screen(receitas)));
setState(() {
isLoading = true;
});
},
child: Text('BUSCAR', style: TextStyle(fontSize: 15.0))):
Center(
child: CircularProgressIndicator(),
),

为了解决这个问题,尝试使用全局变量,如Dart中的全局变量中所述,但它不起作用。


全球达特

library my_prj.globals;
bool isLoading;


主飞镖

import 'globals.dart' as global;
!global.isLoading?FlatButton(...
setState(() {
global.isLoading = true;
});


result_screen.飞镖

import 'globals.dart' as global;
global.isLoading = false;
...

如有必要,我可以显示代码的更多部分。

你不需要全局变量来实现这个使用生命周期方法(deactivate(((

class Temp extends StatefulWidget {
@override
_TempState createState() => _TempState();
}
class _TempState extends State<Temp> {
bool isLoading=false;
void deactivate() {       //Life cycle method
isLoading=false;
super.deactivate();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.yellow,
),
);
}
}

此停用方法将在弹出此小部件时调用,或者在导航到其他页面时正常调用。在停用方法中,我设置了 isLoad=false,所以当 您导航到下一页正在加载变为假。