Reputation: 113
Im working on a Flutter app where I need a SelectableProvider
that accepts a generic type <T>
and manages a list of selected items. The provider is used to manage the state between the GridView
and PageView
pages.
This Provider should be disposable because this Flutter app will allow multiple scenarios for the user to make selections of item <T>
.
While it's not the solution I want, I have it setup as a global ChangeNotifierProvider
where I just clear the state when it's not in use.
Stateless
widgets. And I'm not sure how efficient it would be to allow a Provider as large as it is running in the background with other fairly large Providers I have defined.I've wrapped the PageView
page with with ChangeNotifierProvider.value(value: selectableProvider, ...
but in DevTools Provider tool, it's showing duplicate SelectableProviders. Clearly that's not effective either.
SelectableProvider
class SelectableProvider<T> with ChangeNotifier implements TickerProvider {
final List<T> _selectedItems = [];
late AnimationController _gridAnimationController;
late Animation<double> _gridAnimation;
SelectableProvider() {
_initAnimationControllers();
_initAnimations();
}
/* rest of code */
}
GridView
ChangeNotifierProvider(
create: (_) => SelectableProvider<MyItemType>(),
child: GridView.builder(/* builder */),
)
onPressed To PageView
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChangeNotifierProvider.value(
value: selectableProvider,
builder: (context, child) => PageView(),
),
),
)
So basically, is there any way I can initiate the SelectableProvider inside this specific scope instead of globally above MaterialApp
? And how do I prevent the Provider from duplicating itself to cover both Widgets? Or is that completely normal?
Upvotes: 0
Views: 29