I checked mobile is connect to the internet or not. I used this way. It's working very well. But I used this way every classes. same code duplicated. I don't understand, How to use this kind of code in global.
initialize variable
bool isOffline = false;
initState
@override
void initState() {
ConnectionStatusSingleton connectionStatus =
ConnectionStatusSingleton.getInstance();// connectionStatusSingleton is another class
_connectionChangeStream =
connectionStatus.connectionChange.listen(connectionChanged);
connectionChanged(connectionStatus.hasConnection);
super.initState();
}
connectionChanged method
void connectionChanged(dynamic hasConnection) {
setState(() {
isOffline = !hasConnection;
});
}
After that I used in widget If connection not available I displayed appBar,
appBar: isOffline
? PreferredSize(
preferredSize: Size.fromHeight(20.0),
child: AppBar(
leading: Container(),
centerTitle: true,
backgroundColor: Colors.red,
title: Text(
AppTranslations.of(context).text("connection_drop"),
style: TextStyle(fontSize: 15.0, color: Colors.white),
),
),
)
: null,