-
-
Save eduardoflorence/d918d05ad71175b52c2aca95588c305d to your computer and use it in GitHub Desktop.
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, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sorry, I had tried to use ur code to run, but the lifecycle function was not called any.
Launching lib/main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Debug service listening on ws://127.0.0.1:64955/58RQ_dS_nfw=/ws
Syncing files to device sdk gphone x86 arm...
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
[GETX] GOING TO ROUTE /home
[GETX] Instance "HomeController" has been created
[GETX] Instance "HomeController" has been initialized
[GETX] GOING TO ROUTE /city
[GETX] Instance "CityProvider" has been created
[GETX] Instance "CityProvider" has been initialized
[GETX] Instance "CityController" has been created
[GETX] Instance "CityController" has been initialized
[GETX] CLOSE TO ROUTE /city
[GETX] "CityProvider" onDelete() called
[GETX] "CityProvider" deleted from memory
[GETX] "CityController" onDelete() called
[GETX] "CityController" deleted from memory
Same here - sort of. Things like didPopRoute or didPushRoute are not called; onPaused, onResumed etc. seem to work.
这里也一样-有点。不调用 didPopRoute 或 didPushRoute 之类的东西;onPaused、onResumed 等似乎有效。
mine too
不好使
What is the best extension for visual studio code from json function net working in VS CODE
p.s. this is for the app lifecycle, not for the widget lifecycle. Just point it out in case someone thought this is for widget lifecycle recording
invalid
For those who are having a problem with the declaration of FullLifeCycle, just change the name of the class to FullLifeCycleMixin.