Last active
October 14, 2022 05:42
-
-
Save Lzyct/7ee0ce9f2c7898d7f1892a0d802041c6 to your computer and use it in GitHub Desktop.
Handle online/offline on Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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