使用按钮刷新/重建页面



当用户没有连接时,将显示一条"您处于脱机状态"消息,并在用户恢复连接时为其提供刷新页面的按钮。当用户单击"重试"按钮时,如何刷新或重建页面?它不起作用,是不是出了什么问题?我的代码:

if(hasInternet){
return something}
else{
return NoInternetPage();}


class NoInternetPage extends StatefulWidget {
@override
_NoInternetPageState createState() => _NoInternetPageState();
}
class _NoInternetPageState extends State<NoInternetPage> {
void Refresh(){
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You're offline', style: TextStyle(fontSize: ScreenSize.blockSizeVertical * 3.5, color: CustomColor.success, fontWeight: FontWeight.bold),),
SizedBox(height: ScreenSize.blockSizeVertical * 2),
Padding(
padding:  EdgeInsets.only(left: ScreenSize.blockSizeVertical * 8, right: ScreenSize.blockSizeVertical * 8),
child: Container(
child: Text('Please check your Internet connection and try again.', style: TextStyle(fontSize: ScreenSize.blockSizeVertical * 2.5, ),
textAlign: TextAlign.center,
),
),
),
SizedBox(height: ScreenSize.blockSizeVertical * 4),
FlatButton(
onPressed: (){
Refresh();
},
child: Text(
'Try Again',
style: TextStyle(fontSize: ScreenSize.blockSizeVertical * 2.7, color: Colors.white, fontWeight: FontWeight.bold ),
)
),
],
)
);
}
}

您可以使用Blob状态管理。在区块中检查状态,并使用blocBuilder包装您的按钮、页面或应用程序。如果你不想使用状态管理库或其他什么,你可以使用navigator 2.0,但它并不比bloc更好。

也许您可以使用getX:https://pub.dev/packages/get

有很多解决方案,比如将其转换为StatefulWidget(如果这里没有更多逻辑,则最简单(、状态管理存储库(如ProviderBloC(,或者从调用setState()的第一个父级StatefulWidget传递函数。如果您有一个处理Stream,也可以使用StreamBuilder。这真的取决于你真正需要什么

onPressed函数中调用setState(() {});

最新更新