build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Connectivity Manager Demo",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
scaffoldBackgroundColor: Colors.grey.shade100,
textTheme: const TextTheme(
bodyMedium: TextStyle(fontSize: 16),
),
),
home: Builder(
builder: (context) {
/// Initialize connectivity monitoring
///
/// ConnectivityService().initialize(
// ConnectivityService().initialize(
// context,
// config: ConnectivityConfig(
// disconnectedText: "Oops! No Internet 😢",
// connectedText: "Back Online 🎉",
// // 👇 Custom handler jab net wapas aaye
// ),
// );
ConnectivityService().initialize(
context,
config: ConnectivityConfig(
connectedText: "✅ Back Online 🎉",
disconnectedText: "🚨 Oops! No Internet 😢",
showRetryButton: true,
duration: const Duration(seconds: 3),
onRetry: () {
debugPrint("Retry Pressed (Custom Action)");
},
onConnectedBuilder: (ctx) {
// Example: toast library use kar lo
ScaffoldMessenger.of(ctx).showSnackBar(
const SnackBar(content: Text("✅ Internet Restored with Toast style")),
);
},
/// Custom full-screen page for no internet
customScreenBuilder: (ctx) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Center(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.wifi_off_rounded,
size: 100,
color: Colors.red,
),
const SizedBox(height: 20),
const Text(
"No Internet Connection",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.black87,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 12),
const Text(
"Please check your Wi-Fi or Mobile Data settings.",
style: TextStyle(
fontSize: 16,
color: Colors.black54,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: () {
debugPrint("Retry from custom screen");
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 12,
),
),
icon: const Icon(Icons.refresh, color: Colors.white),
label: const Text(
"Retry",
style: TextStyle(color: Colors.white, fontSize: 16),
),
),
],
),
),
),
),
);
},
),
);
return const HomePage();
},
),
);
}