https://github.com/MinwooRowan/get_it_study
viewmodel은 UI상태관리를 직접적으로 하지 않도록 한다.
UI상태관리는 provider, hooks를 통해서 진행
Router를 통해서 Ref를 주입받고, initializer 제공
abstract class CommonViewmodel {
CommonViewmodel(this.ref) {
initializer();
}
final Ref ref;
Future<Result> initializer();
void dispose();
}
class CombViewmodel extends CommonViewmodel {
CombViewmodel(
super.ref,
UserUsecase userUsecase,
) : _userUsecase = userUsecase {
// Viewmodel 초기화시 진행 함수 작성
getUserList = Command0(_getUserList);
getUserList.execute();
}
final UserUsecase _userUsecase;
// 함수 은닉화를 위한 Command함수 (from Flutter Architecture)
late final Command0<List<UserEntity>> getUserList;
Future<void> initializer() {
return getUserList.execute();
}
void dispose() {
//
}
Future<Result<List<UserEntity>>> _getUserList() async {
//
}
}
abstract class Command<T> extends ChangeNotifier {
Command();
bool running = false;
Result<T>? _result;
/// true if action completed with error
bool get error => _result is Error;
/// true if action completed successfully
bool get completed => _result is Ok;
/// Internal execute implementation
Future<void> _execute(action) async {
if (_running) return;
// Emit running state - e.g. button shows loading state
_running = true;
_result = null;
notifyListeners();
try {
_result = await action();
} finally {
_running = false;
notifyListeners();
}
}
}
노력중
적용
적용전
노력중
if
문):ViewModel의 플래그(boolean) 또는 nullable 필드 값을 기반으로 위젯의 가시성을 제어.
if (viewModel.isLoading) {
return CircularProgressIndicator();
} else {
return Text("Content Loaded");
}
AnimationController
또는 AnimatedWidget
관련 로직.
AnimatedOpacity(
opacity: viewModel.isVisible ? 1.0 : 0.0,
duration: Duration(milliseconds: 300),
child: Text("Fade In Text"),
);
if (MediaQuery.of(context).size.width > 600) {
return LargeScreenWidget();
} else {
return SmallScreenWidget();
}
onPressed: () {
Navigator.pushNamed(context, '/details');
}
적용
일부 진행중
적용
적용
적용전
일부 진행중
적용
적용
적용
적용
적용