connectivity_watcher
A lightning-fast Flutter plugin for internet connectivity monitoring โ with subsecond response times, built-in retry mechanisms, and fully customizable UI for alerts and status widgets.
Custom UI | SnackBar Style | Alert Dialog |
---|---|---|
๐งโ๐ป Getting Started
1. Install the package
dependencies:
flutter:
sdk: flutter
connectivity_watcher: ^[latest_version]
2. Import it
import 'package:connectivity_watcher/connectivity_watcher.dart';
3. Initialize it in main.dart
WidgetsFlutterBinding.ensureInitialized();
ZoConnectivityWatcher().setUp();
๐ Basic Usage
Wrap Your App
Use ZoConnectivityWrapper
at the root of your app to monitor connection changes:
ZoConnectivityWrapper(
connectivityStyle: NoConnectivityStyle.SNACKBAR,
builder: (context, connectionKey) {
return MaterialApp(
navigatorKey: connectionKey,
home: LoginDemo(),
);
},
);
Use Prebuilt UI Styles
Snackbar Style
connectivityStyle: NoConnectivityStyle.SNACKBAR,
Alert Dialog Style
connectivityStyle: NoConnectivityStyle.ALERT,
๐งฉ Custom Offline Widget
Want to show your own offline UI? Use NoConnectivityStyle.CUSTOM
:
ZoConnectivityWrapper(
connectivityStyle: NoConnectivityStyle.CUSTOM,
offlineWidget: CustomNoInternetWrapper(
builder: (context) => CustomNoInternet(),
),
builder: (context, connectionKey) => MaterialApp(
navigatorKey: connectionKey,
home: LoginDemo(),
),
);
The widget is auto-removed once the internet is back. You can also remove it manually:
bool removed = await ZoConnectivityWatcher().hideNoInternet();
if (!removed) {
print("Still no internet");
}
๐ API Call with Retry
Automatically retries failed API calls after connectivity is restored:
ZoConnectivityWatcher().makeApiCallWithRetry(
maxRetries: 2,
delay: const Duration(seconds: 1),
apiCall: () async {
final dio = Dio();
dio.interceptors.add(CurlInterceptor());
final response = await dio.post(
"https://jsonplaceholder.typicode.com/posts",
data: {
"title": 'foo',
"body": 'bar',
"userId": 1,
},
);
},
);
๐ Check Internet Manually
bool hasInternet = await ZoConnectivityWatcher().isInternetAvailable;
๐ง Network-Aware Widgets
Use ZoNetworkAwareWidget
to render UI based on real-time connectivity:
ZoNetworkAwareWidget(
builder: (context, status) {
return Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20),
),
child: MaterialButton(
onPressed: () {},
child: Text(
status == ConnectivityWatcherStatus.connected ? 'Connected' : 'Disconnected',
style: TextStyle(color: Colors.black, fontSize: 25),
),
),
);
},
);
๐งช Curl Logging for Dio
Log API requests as curl commands:
final dio = Dio();
dio.interceptors.add(CurlInterceptor());
๐ ๏ธ Feature Requests & Bugs
Have an idea or found a bug? Open an issue on GitHub.
๐ฆ More From Me
- zo_animated_border: Modern gradient border animations.
- zo_screenshot: Prevent screenshots and record secure areas.
- theme_manager_plus: Manage Flutter themes with custom classes.
- ultimate_extension: Powerful utilities for Dart collections.
- date_util_plus: Simplified date & time utilities.
- pick_color: Extract colors from images by tapping.
Libraries
- connectivity_watcher
- controller/zo_connectivity_controller
- core/interceptors/curl_interceptor
- core/manager/zo_ping_checker
- core/manager/zo_retry_manager
- core/service/zo_connectivity_watcher_service
- core/widgets/dialogue/native_alert
- core/widgets/network_aware/zo_network_aware
- screens/custom_no_internet
- screens/zo_connectivity_watcher_warpper