Created
May 29, 2024 00:03
-
-
Save ashraf267/a468f2c7ee877b88a80819d7a8981980 to your computer and use it in GitHub Desktop.
app wifi icon works but needs fix; on off, it doen't change
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'; | |
import 'package:get/get.dart'; | |
import 'package:android_flutter_wifi/android_flutter_wifi.dart'; | |
import 'package:app_settings/app_settings.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const GetMaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: First(), | |
); | |
} | |
} | |
class First extends StatefulWidget { | |
const First({super.key}); | |
@override | |
State<First> createState() => _FirstState(); | |
} | |
class _FirstState extends State<First> { | |
final UserController _user = Get.put(UserController()); | |
// my code | |
final WCont wifiController = Get.put(WCont()); | |
Future<void> initWifiState() async { | |
await AndroidFlutterWifi.init(); | |
bool wifiOn = await AndroidFlutterWifi.isWifiEnabled(); | |
setState(() { | |
wifiController.wifiOff.value = wifiOn ? Icons.wifi : Icons.wifi_off; | |
}); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
initWifiState(); | |
} | |
// ... | |
//load the user class | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("First"), | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
IconButton( | |
onPressed: () { | |
wifiController.wifiUpdate(); | |
}, | |
icon: Obx( | |
() => Icon(wifiController.wifiOff.value), | |
), | |
), | |
Obx( | |
// the Obx is a function in the getx package. It returns a widget and this function gets called automatically whenever the data changes. | |
() => Text( | |
"I am ${_user.age} years old", | |
textAlign: TextAlign.center, | |
style: const TextStyle( | |
fontSize: 20, | |
), | |
), | |
), | |
const SizedBox(height: 10), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
_user.incrementAge(); | |
}, | |
child: const Text("Increment Age"), | |
), | |
), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
// Get.to(Second()); | |
Get.to(() => Second()); | |
}, | |
child: const Text("Second Page"), | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
class UserController extends GetxController { | |
var name = "Akin".obs; | |
var age = 27.obs; | |
// .obs makes the variable observable and causes the widget that renders it to automatically update when the data changes i.e age = 28 | |
incrementAge() => age++; // function that modifies the variable age | |
} | |
class WCont extends GetxController { | |
Rx<IconData> wifiOff = Icons.wifi_off.obs; | |
@override | |
void onInit() { | |
super.onInit(); | |
wifiUpdate(); | |
} | |
Future<void> wifiUpdate() async { | |
await AndroidFlutterWifi.init(); | |
bool isWifiOn = await AndroidFlutterWifi.isWifiEnabled(); | |
wifiOff = (isWifiOn) ? Icons.wifi.obs : Icons.wifi_off.obs; | |
if (!isWifiOn) { | |
await AppSettings.openAppSettingsPanel(AppSettingsPanelType.wifi); | |
wifiUpdate(); | |
} | |
} | |
} | |
class Second extends StatelessWidget { | |
final UserController _user = Get.find(); | |
Second({super.key}); //find and re-use the already loaded user class | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Second"), | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Obx( | |
() => Text( | |
"I am ${_user.age} years old", | |
textAlign: TextAlign.center, | |
style: const TextStyle( | |
fontSize: 20, | |
), | |
), | |
), | |
const SizedBox(height: 10), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
_user.incrementAge(); | |
}, | |
child: const Text("Increment Age"), | |
), | |
), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
// Get.to(Third()); | |
// Get.to(() => Third()); | |
Get.back(); | |
}, | |
child: const Text("Third Page"), | |
), | |
) | |
], | |
), | |
); | |
} | |
} | |
class Third extends StatelessWidget { | |
final UserController _user = Get.find(); | |
Third({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("Third"), | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Obx( | |
() => Text( | |
"I am ${_user.age} years old", | |
textAlign: TextAlign.center, | |
style: const TextStyle( | |
fontSize: 20, | |
), | |
), | |
), | |
const SizedBox(height: 10), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
_user.incrementAge(); | |
}, | |
child: const Text("Increment Age"), | |
), | |
), | |
Container( | |
alignment: Alignment.center, | |
child: TextButton( | |
onPressed: () { | |
// Get.offAll(First()); | |
Get.offAll(() => First()); | |
}, | |
child: const Text("First Page"), | |
), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment