[Flutter] Provider Essential - 3

jaehee kim·2022년 2월 4일
1

Flutter

목록 보기
19/20
post-thumbnail

ProxyProvider

어떤 Provider에서 다른 Provider를 의존해야하는 경우 사용할 수 있다.

ProxyProvider의 update callback 호출되는 경우

  1. ProxyProvider가 의존하는 Provider의 값을 처음으로 얻었을때
  2. ProxyProvider가 의존하는 Provider의 값이 변경될 때
  3. ProxyProvider가 rebuild될 때

Example 1. ProxyProvider

class Counter with ChangeNotifier {
  int counter = 0;

  void increment() {
    counter++;
    notifyListeners();
  }
}
class Translations {
  const Translations(this._value);
  final int _value;

  String get title => 'You clicked $_value times';
}
class ChgNotiProvProxyProv extends StatefulWidget {
  const ChgNotiProvProxyProv({Key? key}) : super(key: key);

  
  _ChgNotiProvProxyProvState createState() => _ChgNotiProvProxyProvState();
}

class _ChgNotiProvProxyProvState extends State<ChgNotiProvProxyProv> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ChangeNotifierProvider ProxyProvider'),
      ),
      body: Center(
        child: MultiProvider(
          providers: [
            ChangeNotifierProvider<Counter>(
              create: (_) => Counter(),
            ),
            ProxyProvider<Counter, Translations>(
              update: (_, counter, __) => Translations(counter.counter),
            ),
          ],
          child: Column(
            children: [
              ShowTranslations(),
              IncreaseButton(),
            ],
          ),
        ),
      ),
    );
  }
}
class ShowTranslations extends StatelessWidget {
  const ShowTranslations({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    final title = context.watch<Translations>().title;

    return Text(title);
  }
}

class IncreaseButton extends StatelessWidget {
  const IncreaseButton({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => context.read<Counter>().increment(),
      child: Text('INCREASE'),
    );
  }
}

Example 2. ChangeNotifierProxyProvider

class Counter with ChangeNotifier {
  int counter = 0;

  void increment() {
    counter++;
    notifyListeners();
  }
}
class Translations with ChangeNotifier {
  late int _value;

  void update(Counter counter) {
    _value = counter.counter;
    notifyListeners();
  }

  String get title => 'You clicked $_value times';
}
class ChgNotiProvChgNotiProxyProv extends StatefulWidget {
  const ChgNotiProvChgNotiProxyProv({Key? key}) : super(key: key);

  
  _ChgNotiProvChgNotiProxyProvState createState() =>
      _ChgNotiProvChgNotiProxyProvState();
}

class _ChgNotiProvChgNotiProxyProvState
    extends State<ChgNotiProvChgNotiProxyProv> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ChangeNotifierProvider ChagneNotifierProxyProvider'),
      ),
      body: Center(
        child: MultiProvider(
          providers: [
            ChangeNotifierProvider<Counter>(
              create: (_) => Counter(),
            ),
            ChangeNotifierProxyProvider<Counter, Translations>(
              create: (_) => Translations(),
              update: (
                BuildContext _,
                Counter counter,
                Translations? translations,
              ) {
                translations!..update(counter);
                return translations;
              },
            ),
          ],
          child: Column(
            children: [
              ShowTranslations(),
              IncreaseButton(),
            ],
          ),
        ),
      ),
    );
  }
}
class ShowTranslations extends StatelessWidget {
  const ShowTranslations({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    final title = context.watch<Translations>().title;

    return Text(title);
  }
}

class IncreaseButton extends StatelessWidget {
  const IncreaseButton({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => context.read<Counter>().increment(),
      child: Text('INCREASE'),
    );
  }
}

ChangeNotifierProxyProvider, ProxyProvider 차이

ProxyProvider에서는 instance를 계속 생성한다.





Reference

Heavy Fran - Flutter Provider Essential

0개의 댓글