어떤 Provider에서 다른 Provider를 의존해야하는 경우 사용할 수 있다.
- ProxyProvider가 의존하는 Provider의 값을 처음으로 얻었을때
- ProxyProvider가 의존하는 Provider의 값이 변경될 때
- ProxyProvider가 rebuild될 때
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'),
);
}
}
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'),
);
}
}
ProxyProvider에서는 instance를 계속 생성한다.