[Flutter] GetX-GetView

kimdocs...📄·2023년 12월 1일
0

flutter

목록 보기
22/30

GetView?

: 보다 간결하게 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');
            ); 
        );
    }
}
profile
👩‍🌾 GitHub: ezidayzi / 📂 Contact: ezidayzi@gmail.com

0개의 댓글