Flutter 显示一条消息";没有互联网“;如果在Flatter android中使用webview页面时失去互联网连接 bool connectionStatus=true; Future check()异步{ 试一试{ 最终结果=等待InternetAddress.lookup('google.com'); if(result.isNotEmpty&&result[0].rawAddress.isNotEmpty){ connectionStatus=true; } }关于SocketException捕获(41;{ connectionStatus=false; } } 类BackButtonState扩展状态{ DateTime backbuttonpressedTime; @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:首选大小( preferredSize:大小(double.infinity,0), 孩子:AppBar( 标题:文本(“”), 背景颜色:颜色。深紫色, ), ), 正文:未来建设者( future:check(),//以前获得的future或null 生成器:(BuildContext上下文,异步快照){ 如果(connectionStatus==true){ 返回式示波器( onWillPop:onWillPop, 孩子:WebviewScaffold( 网址:'https://www.theonlineindia.co.in', 隐藏:是的, initialChild:容器( 孩子:康斯特中心( 子对象:循环压缩机指示器( valueColor: 始终为蓝色(颜色为深紫色)), ), ), ), ); }否则{ 返回中心( 子:列( mainAxisAlignment:mainAxisAlignment.center, crossAxisAlignment:crossAxisAlignment.center, 儿童:[ 容器( 孩子:文本('没有互联网连接!!!', 样式:TextStyle( //你的文字 fontFamily:“Aleo”, fontStyle:fontStyle.normal, fontWeight:fontWeight.bold, 字体大小:25.0, )), ), /*升起的按钮( 已按下:(){ setState((){}); }, 颜色:颜色(0xFF673AB7), textColor:Colors.white, 子项:文本(“刷新”), ), */ //您的按钮位于文本下方 ], )); } }), ); } Future onWillPop()异步{ DateTime currentTime=DateTime.now(); //如果BackButton未打开,则系统将关闭 //报表1或报表2 bool backButton=backbuttonpressedTime==null|| 当前时间差(backbuttonpressedTime)>持续时间(秒:2); 如果(后退按钮){ backbuttonpressedTime=当前时间; 烤面包片( msg:“双击退出应用程序”, 背景颜色:颜色。深紫色, 文本颜色:颜色。白色); 返回false; } 出口(0); 返回true; } }

Flutter 显示一条消息";没有互联网“;如果在Flatter android中使用webview页面时失去互联网连接 bool connectionStatus=true; Future check()异步{ 试一试{ 最终结果=等待InternetAddress.lookup('google.com'); if(result.isNotEmpty&&result[0].rawAddress.isNotEmpty){ connectionStatus=true; } }关于SocketException捕获(41;{ connectionStatus=false; } } 类BackButtonState扩展状态{ DateTime backbuttonpressedTime; @凌驾 小部件构建(构建上下文){ 返回脚手架( appBar:首选大小( preferredSize:大小(double.infinity,0), 孩子:AppBar( 标题:文本(“”), 背景颜色:颜色。深紫色, ), ), 正文:未来建设者( future:check(),//以前获得的future或null 生成器:(BuildContext上下文,异步快照){ 如果(connectionStatus==true){ 返回式示波器( onWillPop:onWillPop, 孩子:WebviewScaffold( 网址:'https://www.theonlineindia.co.in', 隐藏:是的, initialChild:容器( 孩子:康斯特中心( 子对象:循环压缩机指示器( valueColor: 始终为蓝色(颜色为深紫色)), ), ), ), ); }否则{ 返回中心( 子:列( mainAxisAlignment:mainAxisAlignment.center, crossAxisAlignment:crossAxisAlignment.center, 儿童:[ 容器( 孩子:文本('没有互联网连接!!!', 样式:TextStyle( //你的文字 fontFamily:“Aleo”, fontStyle:fontStyle.normal, fontWeight:fontWeight.bold, 字体大小:25.0, )), ), /*升起的按钮( 已按下:(){ setState((){}); }, 颜色:颜色(0xFF673AB7), textColor:Colors.white, 子项:文本(“刷新”), ), */ //您的按钮位于文本下方 ], )); } }), ); } Future onWillPop()异步{ DateTime currentTime=DateTime.now(); //如果BackButton未打开,则系统将关闭 //报表1或报表2 bool backButton=backbuttonpressedTime==null|| 当前时间差(backbuttonpressedTime)>持续时间(秒:2); 如果(后退按钮){ backbuttonpressedTime=当前时间; 烤面包片( msg:“双击退出应用程序”, 背景颜色:颜色。深紫色, 文本颜色:颜色。白色); 返回false; } 出口(0); 返回true; } },flutter,webview,Flutter,Webview,在上面的代码中,我只能在启动应用程序时显示消息“No internet”。 我想在应用程序启动后显示相同的消息,如果在访问webview中的网页时internet连接丢失 我更愿意通过代码本身实现上述功能,而不是寻找任何插件。插件将是第二选择 *选项1 定义定期检查连接的计时器 bool connectionStatus = true; Future check() async { try { final result = await InternetAddress.lookup('

在上面的代码中,我只能在启动应用程序时显示消息“No internet”。 我想在应用程序启动后显示相同的消息,如果在访问webview中的网页时internet连接丢失

我更愿意通过代码本身实现上述功能,而不是寻找任何插件。插件将是第二选择

*

选项1 定义定期检查连接的计时器

bool connectionStatus = true;
Future check() async {
  try {
    final result = await InternetAddress.lookup('google.com');
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      connectionStatus = true;
    }
  } on SocketException catch (_) {
    connectionStatus = false;
  }
}

class _BackButtonState extends State<BackButton> {
  DateTime backbuttonpressedTime;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(double.infinity, 0),
        child: AppBar(
          title: Text(""),
          backgroundColor: Colors.deepPurple,
        ),
      ),
      body: FutureBuilder(
          future: check(), // a previously-obtained Future or null
          builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
            if (connectionStatus == true) {
              return WillPopScope(
                onWillPop: onWillPop,
                child: WebviewScaffold(
                  url: 'https://www.theonlineindia.co.in',
                  hidden: true,
                  initialChild: Container(
                    child: const Center(
                      child: CircularProgressIndicator(
                          valueColor:
                              AlwaysStoppedAnimation<Color>(Colors.deepPurple)),
                    ),
                  ),
                ),
              );
            } else {
              return Center(
                  child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Container(
                    child: Text('No internet connection !!!',
                        style: TextStyle(
                          // your text
                          fontFamily: 'Aleo',
                          fontStyle: FontStyle.normal,
                          fontWeight: FontWeight.bold,
                          fontSize: 25.0,
                        )),
                  ),
                  /* RaisedButton(
                    onPressed: () {
                      setState(() {});
                    },
                    color: Color(0xFF673AB7),
                    textColor: Colors.white,
                    child: Text('Refresh'),
                  ), */
                  // your button beneath text
                ],
              ));
            }
          }),
    );
  }

  Future<bool> onWillPop() async {
    DateTime currentTime = DateTime.now();

    //ifbackbuttonhasnotbeenpreedOrToasthasbeenclosed
    //Statement 1 Or statement2
    bool backButton = backbuttonpressedTime == null ||
        currentTime.difference(backbuttonpressedTime) > Duration(seconds: 2);
    if (backButton) {
      backbuttonpressedTime = currentTime;
      Fluttertoast.showToast(
          msg: "Double tap to exit the app",
          backgroundColor: Colors.deepPurple,
          textColor: Colors.white);
      return false;
    }
    exit(0);
    return true;
  }
}
选择2 使用插件并在应用程序主窗口小部件中定义流

  const period= const Duration(seconds:5);
  new Timer.periodic(period, (Timer t) => check());
StreamSubscription网络订阅;
@凌驾
void initState(){
super.initState();
networkSubscription=Connectivity().onConnectivityChanged.listen((ConnectivityResult结果){
检查();
});
checkNetwork();//开始检查时需要
} 

非常感谢,这对我很有效。Timer.periodic(新持续时间(秒:10),(时间)异步{final flatterwebviewplugin=flatterwebviewplugin();if(connectionStatus==false)flatterwebviewplugin.reload();尝试{final result=wait InternetAddress.lookup('google.com');if(result.isNotEmpty&&result[0].rawAddress.isNotEmpty)SocketException捕获({connectionStatus=true;}}}on SocketException捕获({connectionStatus=false;flatterToast.showtoos(msg:“无internet连接”);})@atifshaikh不客气,很高兴它能帮上忙!你能把答案标记为已接受吗?如果它完全回答了你的问题,当然我找不到这样做的选项。@atifshaikh
    StreamSubscription<ConnectivityResult> networkSubscription;
    @override
      void initState() {
        super.initState();
        networkSubscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
          check();
        });
        checkNetwork(); // Needed for the check on start
      }