Created
April 8, 2021 15:23
-
-
Save RipplesCode/8b2e5d67feae05fed8183217045d9d13 to your computer and use it in GitHub Desktop.
Bindings
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_getx/home_controller.dart'; | |
import 'package:flutter_getx/my_controller.dart'; | |
import 'package:get/get.dart'; | |
class AllControllerBinding implements Bindings { | |
@override | |
void dependencies() { | |
Get.lazyPut<MyController>(() => MyController()); | |
Get.lazyPut<HomeController>(() => HomeController()); | |
} | |
} |
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:flutter_getx/home_controller.dart'; | |
import 'package:flutter_getx/main.dart'; | |
import 'package:get/get.dart'; | |
class Home extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Home"), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Obx(() => Text( | |
'The value is ${Get.find<HomeController>().count}', | |
style: TextStyle(fontSize: 25), | |
)), | |
RaisedButton( | |
child: Text("Increment"), | |
onPressed: () { | |
Get.find<HomeController>().increment(); | |
}, | |
), | |
SizedBox( | |
height: 10, | |
), | |
RaisedButton( | |
child: Text("Back"), | |
onPressed: () { | |
Get.back(); | |
}, | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
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:get/get.dart'; | |
class HomeController extends GetxController { | |
var count=0.obs; | |
void increment() { | |
count++; | |
} | |
} |
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_getx/home_controller.dart'; | |
import 'package:get/get.dart'; | |
class HomeControllerBinding implements Bindings { | |
@override | |
void dependencies() { | |
Get.lazyPut<HomeController>(() => HomeController()); | |
} | |
} |
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:flutter_getx/all_controller_binding.dart'; | |
import 'package:flutter_getx/home.dart'; | |
import 'package:flutter_getx/home_controller_binding.dart'; | |
import 'package:flutter_getx/my_controller.dart'; | |
import 'package:flutter_getx/myapp_controller_binding.dart'; | |
import 'package:flutter_getx/service.dart'; | |
import 'package:get/get.dart'; | |
void main() { | |
//MyAppControllerBinding().dependencies(); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
// TODO: implement build | |
return GetMaterialApp( | |
// initialBinding: AllControllerBinding(), | |
title: "Binding", | |
// If binding applied at route level | |
// GetPage( | |
// name: '/home', | |
// page: () => Home(), | |
// binding: HomeControllerBinding(), | |
// ), | |
// ], | |
//Binding Builder(If we dont want to use separate binding class) | |
// getPages: [ | |
// GetPage( | |
// name: '/home', | |
// page: () => Home(), | |
// binding: BindingsBuilder(() => { | |
// Get.lazyPut<HomeControllerBinding>( | |
// () => HomeControllerBinding()) | |
// }), | |
// ), | |
// ], | |
home: Scaffold( | |
appBar: AppBar(title: Text("Binding")), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Obx(() => Text( | |
'The value is ${Get.find<MyController>().count}', | |
style: TextStyle(fontSize: 25), | |
)), | |
RaisedButton( | |
child: Text("Increment"), | |
onPressed: () { | |
Get.find<MyController>().increment(); | |
}, | |
), | |
SizedBox( | |
height: 10, | |
), | |
RaisedButton( | |
child: Text("Home"), | |
onPressed: () { | |
// Get.to(Home()); | |
//For named route | |
//Get.toNamed("/home"); | |
//for normal routes | |
Get.to(Home(), binding: HomeControllerBinding()); | |
}, | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
// Smart Management | |
//GetX provides SmartManagement that lets us configure how dependencies | |
// behave in terms of memory management. | |
// GetMaterialApp { | |
// smartManagement: SmartManagement.full // or .keepFactory or .onlyBuilder | |
// } | |
// Full Mode | |
// =========== | |
// Everything gets disposed as soon as the route is removed from navigation stack, | |
// unless declared permanent. | |
// SmartManagement.keepFactory | |
//================================ | |
// Just like SmartManagement.full, it will remove it's dependencies when ' | |
// 'it's not being used anymore. However, it will keep their factory, | |
// which means it will recreate the dependency if you need that instance again. | |
// SmartManagement.onlyBuilders | |
//================================ | |
// With this option, only controllers started in init: or loaded into a Binding | |
// with Get.lazyPut() will be disposed. | |
// | |
// If you use Get.put() or Get.putAsync() or any other approach, | |
// SmartManagement will not have permissions to exclude this dependency. |
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:get/get.dart'; | |
class MyController extends GetxController { | |
var count=0.obs; | |
void increment() { | |
count++; | |
} | |
} |
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_getx/my_controller.dart'; | |
import 'package:get/get.dart'; | |
class MyAppControllerBinding implements Bindings { | |
@override | |
void dependencies() { | |
Get.lazyPut<MyController>(() => MyController()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment