: 보다 간결하게 GetxController나 GetxService를 StatelessWidget에 추가할 수 있다.
Binding
class PasswordInputBinding extends Bindings {
dependencies() {
Get.put(PasswordInputController(AuthRepositoryImpl()));
}
}
Controller
class PasswordInputController extends GetxController {
final password= ''.obs;
}
Widget
class PasswordInputPage extends GetView<SomeController> {
const PasswordInputPage ({Key? key}) :super(key: key);
// 이 코드는 이제 사용하지 않아도 된다.
// final controller = Get.put(SomeController());
Widget build(BuildContext context) {
return Container(
child: Text(controller.password.value);
);
}
}
호출
class SomePage extends StatelessWidget {
const SomePage({Key? key}) :super(key: key);
Widget build(BuildContext context) {
return Container(
child: TextButton(
onPressed: () => Get.to(
PasswordInputPage (),
binding: PasswordInputBinding (), // Get.to가 실행될 때 SomeBinding에서 dependencies()함수를 호출함.
);
child: const Text('Go to Some Widget');
);
);
}
}