Last active
November 14, 2024 06:21
-
-
Save eduardoflorence/d918d05ad71175b52c2aca95588c305d to your computer and use it in GitHub Desktop.
Getx - Sample SuperController and FullLifeCycleController
This file contains 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'; | |
void main() { | |
runApp(GetMaterialApp( | |
initialRoute: '/home', | |
getPages: [ | |
GetPage( | |
name: '/home', | |
page: () => HomePage(), | |
binding: HomeBinding(), | |
), | |
GetPage( | |
name: '/city', | |
page: () => CityPage(), | |
binding: CityBinding(), | |
), | |
], | |
)); | |
} | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('HOME')), | |
body: Center( | |
child: ElevatedButton( | |
onPressed: () => Get.toNamed('/city'), | |
child: Text('Go to Cities'), | |
), | |
), | |
); | |
} | |
} | |
class HomeBinding extends Bindings { | |
@override | |
void dependencies() { | |
Get.put(HomeController()); | |
} | |
} | |
class HomeController extends FullLifeCycleController with FullLifeCycle { | |
// Mandatory | |
@override | |
void onDetached() { | |
print('HomeController - onDetached called'); | |
} | |
// Mandatory | |
@override | |
void onInactive() { | |
print('HomeController - onInative called'); | |
} | |
// Mandatory | |
@override | |
void onPaused() { | |
print('HomeController - onPaused called'); | |
} | |
// Mandatory | |
@override | |
void onResumed() { | |
print('HomeController - onResumed called'); | |
} | |
// Optional | |
@override | |
Future<bool> didPushRoute(String route) { | |
print('HomeController - the route $route will be open'); | |
return super.didPushRoute(route); | |
} | |
// Optional | |
@override | |
Future<bool> didPopRoute() { | |
print('HomeController - the current route will be closed'); | |
return super.didPopRoute(); | |
} | |
// Optional | |
@override | |
void didChangeMetrics() { | |
print('HomeController - the window size did change'); | |
super.didChangeMetrics(); | |
} | |
// Optional | |
@override | |
void didChangePlatformBrightness() { | |
print('HomeController - platform change ThemeMode'); | |
super.didChangePlatformBrightness(); | |
} | |
} | |
class CityBinding extends Bindings { | |
@override | |
void dependencies() { | |
Get.lazyPut(() => CityProvider()); | |
Get.put(CityController(cityProvider: Get.find())); | |
} | |
} | |
class CityModel { | |
CityModel({ | |
required this.abbreviation, | |
required this.name, | |
}); | |
String abbreviation; | |
String name; | |
factory CityModel.fromJson(Map<String, dynamic> json) => CityModel( | |
abbreviation: json["sigla"], | |
name: json["nome"], | |
); | |
static List<CityModel> listFromJson(list) => | |
List<CityModel>.from(list.map((x) => CityModel.fromJson(x))); | |
} | |
class CityController extends SuperController<List<CityModel>> { | |
final CityProvider cityProvider; | |
CityController({required this.cityProvider}); | |
@override | |
void onInit() { | |
findAllCities(); | |
super.onInit(); | |
} | |
void findAllCities() { | |
cityProvider.getCity().then((result) { | |
// List<CityModel> data = CityModel.listFromJson(result); | |
List<CityModel>? data = result.body; | |
//print(result); | |
change(data, status: RxStatus.success()); | |
}, onError: (err) { | |
change(null, status: RxStatus.error(err.toString())); | |
}); | |
} | |
// Mandatory | |
@override | |
void onDetached() { | |
print('CityController - onDetached called'); | |
} | |
// Mandatory | |
@override | |
void onInactive() { | |
print('CityController - onInative called'); | |
} | |
// Mandatory | |
@override | |
void onPaused() { | |
print('CityController - onPaused called'); | |
} | |
// Mandatory | |
@override | |
void onResumed() { | |
print('CityController - onResumed called'); | |
} | |
} | |
class CityProvider extends GetConnect { | |
@override | |
void onInit() { | |
// All request will pass to jsonEncode so CasesModel.fromJson() | |
httpClient.defaultDecoder = CityModel.listFromJson; | |
httpClient.addRequestModifier<void>((request) { | |
request.headers['Authorization'] = 'Bearer sdfsdfgsdfsdsdf12345678'; | |
return request; | |
}); | |
} | |
Future<Response<List<CityModel>>> getCity() => get<List<CityModel>>( | |
'https://servicodados.ibge.gov.br/api/v1/localidades/estados', | |
decoder: CityModel.listFromJson); | |
} | |
class CityPage extends GetView<CityController> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Cities')), | |
body: Container( | |
child: controller.obx( | |
(state) => ListView.builder( | |
itemCount: state?.length, | |
itemBuilder: (context, index) { | |
return Text(state?[index].name ?? ''); | |
}, | |
), | |
onLoading: Center(child: CircularProgressIndicator()), | |
onError: (error) => Center( | |
child: Text( | |
'Error: $error', | |
style: TextStyle(fontSize: 18), | |
textAlign: TextAlign.center, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
invalid