Created
May 2, 2022 15:06
-
-
Save romanejaquez/bbe1cee7d5c69cc0fce3b1af6bc8ba0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp( | |
Provider( | |
create: (context) => AppValueNotifier(), | |
child: MyApp() | |
) | |
); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Column( | |
children: [ | |
TriggerWidget(), | |
Expanded( | |
child: MyWidget(), | |
) | |
] | |
), | |
), | |
); | |
} | |
} | |
class Food { | |
final String name; | |
Food({ required this.name }); | |
} | |
class AppValueNotifier | |
{ | |
ValueNotifier<List<Food>> valueNotifier = ValueNotifier([]); | |
void updateDealsList(List<Food> list) | |
{ | |
valueNotifier.value = list; | |
print('DEAL LIST IN CLASS: ${ valueNotifier.value}'); | |
} | |
List<Food> getDealList() | |
{ | |
return valueNotifier.value; | |
} | |
} | |
class TriggerWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
AppValueNotifier appValueNotifier = Provider.of<AppValueNotifier>(context, listen: false); | |
return TextButton( | |
child: const Text('Trigger Adding Items!'), | |
onPressed: () { | |
appValueNotifier.updateDealsList([ | |
Food(name: 'Food 1'), | |
Food(name: 'Food 2'), | |
]); | |
}, | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
AppValueNotifier appValueNotifier = Provider.of<AppValueNotifier>(context, listen: false); | |
return ValueListenableBuilder( | |
valueListenable: appValueNotifier.valueNotifier, | |
builder: (context, List<Food> value, widget) { | |
var list = value; | |
return ListView.builder( | |
itemCount: list.length, | |
itemBuilder: (context, index) { | |
return Text(list[index].name); | |
} | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment