Skip to content

Instantly share code, notes, and snippets.

@Lzyct
Last active October 14, 2022 05:42
Show Gist options
  • Select an option

  • Save Lzyct/7ee0ce9f2c7898d7f1892a0d802041c6 to your computer and use it in GitHub Desktop.

Select an option

Save Lzyct/7ee0ce9f2c7898d7f1892a0d802041c6 to your computer and use it in GitHub Desktop.
Handle online/offline on Flutter
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
color: isOnline(Colors.red, Colors.blue),
child: isOnline(MyWidget(), Container(color: Colors.blue)),
),
),
);
}
T isOnline<T>(T online, T offline) {
/// Check online/offline
bool isOnline = false;
return isOnline ? online : offline;
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'Hello, World!',
style: Theme.of(context).textTheme.headline4,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment