Skip to content

Commit

Permalink
✨ Add none connectivity dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Feb 19, 2020
1 parent a9b4145 commit 64fa916
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/constants/instances.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Expand All @@ -22,6 +23,7 @@ class Instances {
static final EventBus eventBus = EventBus();
static final navigatorKey = GlobalKey<NavigatorState>();
static AppLifecycleState appLifeCycleState = AppLifecycleState.resumed;
static ConnectivityResult connectivityResult;

static final appsPageStateKey = GlobalKey<AppsPageState>();
static final courseSchedulePageStateKey = GlobalKey<CourseSchedulePageState>();
Expand Down
79 changes: 73 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
Expand Down Expand Up @@ -48,23 +51,30 @@ class OpenJMUApp extends StatefulWidget {
}

class OpenJMUAppState extends State<OpenJMUApp> with WidgetsBindingObserver {
final connectivitySubscription = Connectivity().onConnectivityChanged.listen(
(ConnectivityResult result) {
Instances.eventBus.fire(ConnectivityChangeEvent(result));
debugPrint('Current connectivity: $result');
},
);
StreamSubscription connectivitySubscription;

int initAction;

Brightness get _platformBrightness => Screens.mediaQuery.platformBrightness ?? Brightness.light;

ToastFuture connectivityToastFuture;

@override
void initState() {
debugPrint('Current platform is: ${Platform.operatingSystem}');
WidgetsBinding.instance.addObserver(this);
tryRecoverLoginInfo();

Connectivity().checkConnectivity().then(checkIfNoConnectivity);
connectivitySubscription = Connectivity().onConnectivityChanged.listen(
(ConnectivityResult result) {
checkIfNoConnectivity(result);
Instances.eventBus.fire(ConnectivityChangeEvent(result));
Instances.connectivityResult = result;
debugPrint('Current connectivity: $result');
},
);

Instances.eventBus
..on<TicketGotEvent>().listen((event) {
if (!currentUser.isTeacher) {
Expand Down Expand Up @@ -159,6 +169,63 @@ class OpenJMUAppState extends State<OpenJMUApp> with WidgetsBindingObserver {
));
}

void checkIfNoConnectivity(ConnectivityResult result) {
if (result == ConnectivityResult.none) {
if (mounted) {
connectivityToastFuture = showToastWidget(
noConnectivityWidget,
duration: 999.weeks,
handleTouch: true,
);
} else {
SchedulerBinding.instance.addPostFrameCallback((_) {
connectivityToastFuture = showToastWidget(
noConnectivityWidget,
duration: 999.weeks,
handleTouch: true,
);
});
}
} else {
if (Instances.connectivityResult == ConnectivityResult.none) {
connectivityToastFuture?.dismiss(showAnim: true);
}
}
}

Widget get noConnectivityWidget => Material(
color: Colors.black26,
child: BackdropFilter(
filter: ui.ImageFilter.blur(sigmaX: 2.0, sigmaY: 2.0),
child: Center(
child: Container(
width: Screens.width / 2,
height: Screens.width / 2,
padding: EdgeInsets.all(20.0),
decoration: BoxDecoration(
color: Theme.of(context).canvasColor,
shape: BoxShape.circle,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
Icons.router,
size: Screens.width / 6,
color: Theme.of(context).iconTheme.color,
),
SizedBox(height: Screens.width / 20),
Text(
'检查网络连接',
style: Theme.of(context).textTheme.body1.copyWith(fontSize: 20.0),
),
],
),
),
),
),
);

@override
Widget build(BuildContext context) {
return MultiProvider(
Expand Down

0 comments on commit 64fa916

Please sign in to comment.