inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Flutter 앱 개발 실전

riverpod 코드 변환 질문

해결된 질문

312

Link

작성한 질문수 15

1

안녕하세요.

기회가 되신다면 코드 전체를 riverpod 버전으로 변경해서 notion 에 올려주실 수 있으실까요?

riverpod 버전으로 변경해보면서 Dart 문법에 대해 더 이해하게 되는 거 같습니다.

 

코드변환 질문1

class LangService with ChangeNotifier {
  /// 현재 언어
  Locale locale;

  LangService({
    Locale? locale,
  }) : locale = locale ?? IntlHelper.en;

  /// 언어 변경
  void toggleLang() {
    locale = IntlHelper.isKo ? IntlHelper.en : IntlHelper.ko;
    notifyListeners();
  }
}

위 코드를

final langProvider = NotifierProvider<LangNotifier, Locale?> (LangNotifier.new);

class LangNotifier extends Notifier<Locale?> {
  @override
  Locale? build() => IntlHelper.en;

  /// 언어 변경
  void toggleLang() {
    state = IntlHelper.isKo ? IntlHelper.en : IntlHelper.ko;
  }

}

로 변경하면 맞나요?

Notifier<Locale> 로 해야 하는지?

Notifier<Locale?> 로 해야 하는지요?

 

코드변환 질문2

class ThemeService with ChangeNotifier {
  ThemeService({
    AppTheme? theme,
  }) : theme = theme ?? LightTheme();

  /// 현재 테마
  AppTheme theme;

  /// 테마 변경
  void toggleTheme() {
    if (theme.brightness == Brightness.light) {
      theme = DarkTheme();
    } else {
      theme = LightTheme();
    }
    notifyListeners();
  }

  /// Material ThemeData 커스텀
  ThemeData get themeData {
    return ThemeData(
      /// Scaffold
      scaffoldBackgroundColor: theme.color.surface,

      /// AppBar
      appBarTheme: AppBarTheme(
        backgroundColor: theme.color.surface,
        elevation: 0,
        centerTitle: false,
        iconTheme: IconThemeData(
          color: theme.color.text,
        ),
        titleTextStyle: theme.typo.headline2.copyWith(
          color: theme.color.text,
        ),
      ),

      /// BottomSheet
      bottomSheetTheme: const BottomSheetThemeData(
        backgroundColor: Colors.transparent,
        constraints: BoxConstraints(
          maxWidth: Breakpoints.bottomSheet,
        ),
      ),
    );
  }
}

extension ThemeServiceExt on BuildContext {
  ThemeService get themeService => watch<ThemeService>();
  AppTheme get theme => themeService.theme;
  AppColor get color => theme.color;
  AppDeco get deco => theme.deco;
  AppTypo get typo => theme.typo;
}

위 코드를

final themeProvider = NotifierProvider<ThemeNotifier, AppTheme>(ThemeNotifier.new);

class ThemeNotifier extends Notifier<AppTheme> {
  @override
  AppTheme build() => LightTheme();

  AppTheme get theme => state;
  AppColor get color => state.color;
  AppDeco get deco => state.deco;
  AppTypo get typo => state.typo;

  /// 테마 변경
  void toggleTheme() {
    if (state.brightness == Brightness.light) {
      state = DarkTheme();
    } else {
      state = LightTheme();
    }
  }

  /// Material ThemeData 커스텀
  ThemeData get themeData {
    return ThemeData(
      /// Scaffold
      scaffoldBackgroundColor: state.color.surface,

      /// AppBar
      appBarTheme: AppBarTheme(
        backgroundColor: state.color.surface,
        elevation: 0,
        centerTitle: false,
        iconTheme: IconThemeData(
          color: state.color.text,
        ),
        titleTextStyle: state.typo.headline2.copyWith(
          color: state.color.text,
        ),
      ),

      /// BottomSheet
      bottomSheetTheme: const BottomSheetThemeData(
        backgroundColor: Colors.transparent,
        constraints: BoxConstraints(
          maxWidth: Breakpoints.bottomSheet,
        ),
      ),
    );
  }

}

로 변경하면 맞는지요?

flutter

답변 1

1

DevStory

안녕하세요.

 

Q1. 기회가 되신다면 코드 전체를 riverpod 버전으로 변경해서 notion 에 올려주실 수 있으실까요?

Flutter 앱 개발 실전 MVVM & Test 파트 강의 자료 마지막에 Riverpod 버전 링크를 추가하였습니다.

 

Q2. Notifier<Locale> vs Notifier<Locale?>

Locale?는 후속 로직에서 null인 경우에 대한 처리를 계속 해줘야하므로 Locale로 처리하는 것을 권장드립니다.

 

Q3. ThemeService Riverpod 변환

아래 내용을 참고해 주세요.

final themeServiceProvider =
    NotifierProvider<ThemeService, AppTheme>(ThemeService.new);

class ThemeService extends Notifier<AppTheme> {
  @override
  AppTheme build() => LightTheme();

  /// 테마 변경
  void toggleTheme() {
    state = state.brightness == Brightness.light ? DarkTheme() : LightTheme();
  }

  /// Material ThemeData 커스텀
  ThemeData get themeData {
    return ThemeData(
      /// Scaffold
      scaffoldBackgroundColor: state.color.surface,

      /// AppBar
      appBarTheme: AppBarTheme(
        backgroundColor: state.color.surface,
        elevation: 0,
        centerTitle: false,
        iconTheme: IconThemeData(
          color: state.color.text,
        ),
        titleTextStyle: state.typo.headline2.copyWith(
          color: state.color.text,
        ),
      ),

      /// BottomSheet
      bottomSheetTheme: const BottomSheetThemeData(
        backgroundColor: Colors.transparent,
        constraints: BoxConstraints(
          maxWidth: Breakpoints.bottomSheet,
        ),
      ),
    );
  }
}

extension ThemeServiceExt on WidgetRef {
  ThemeService get themeService => watch(themeServiceProvider.notifier);
  AppTheme get theme => watch(themeServiceProvider);
  AppColor get color => theme.color;
  AppDeco get deco => theme.deco;
  AppTypo get typo => theme.typo;
}

감사합니다 :)

1

Link

너무 너무 감사드립니다.

riverpod Full 버전을 올려주셔서 정말 많은 도움이 되었습니다.

BaseViewModel , BaseViewState 구현 부분을 이해하는데 어려움이 있지만 계속 보면서 학습하겠습니다.

1

DevStory

기존 Provider 버전에서 구현한 View 공통 로직을 유사하게 구현하기 위해 BaseViewModel과 BaseViewState를 만들었지만, 코드에 정답은 없기 때문에 참고만 해주세요 :)

수강 기한 연장 요청드려도될까요..

1

48

2

37.provider 실습 문제점, 카트에서 상품이 지워지지 않습니다.

1

76

2

다트 프로젝트

1

51

2

context.read<LangService>().toggleLang 해도 언어가 변경되는 이유

1

74

3

수강 기간 연장 신청 요청드립니다.

1

68

3

수강기간 연장 부탁드립니다.

1

58

3

제공해주신 flutter_design_system 라이브러리 질문입니다.

1

53

2

수강 기간 연장 부탁드립니다

1

52

2

수강 기한 연장

1

78

3

강의 잘 보고있습니다!

1

59

2

애뮬레이터 실행 오류

1

69

2

pdf 강의노트

1

62

2

수강기간 연장 부탁드립니다.

1

86

2

수강 기간 연장 요청

1

86

2

수강기간 연장 부탁드립니다

1

129

2

코드 생성기 - build runner 관련 오류

1

111

1

디자인 시스템 구성에 대해 질문 드립니다

2

145

2

CartItem 추가시

1

95

2

const 커스텀클래스

1

95

1

강의 수강 기간 연장 요청드립니다.

1

127

2

코드 생성기 - 실습 build runner 안 되는 분.

1

270

2

Flutter 강의자료 열리지 않는 문제

1

165

2

riverpod 프로젝트에 궁금한점이 있어 질문 남깁니다.

1

123

2

수강 기강 연장 부탁드리겠습니다! :ㅇ

1

88

2