I have these two calls to makeChild(...)
in a widget. On top of the tree there's a ChangeNotifierProvider
, and inside the makeChild
function I'm accessing the data model via Provider.of<...>(context)
.
The problem is that inside the widget generated by the second call it works fine, but in the first one (the call to showModalBottomSheet
) it throws an error saying that the correct provider couldn't be found above this widget
.
import 'package:flutter/material.dart';
class PuzzleBox extends StatelessWidget {
const PuzzleBox({
Key? key,
required this.makeChild,
}) : super(key: key);
final Function makeChild;
@override
Widget build(BuildContext context) {
return Expanded(
child: GestureDetector(
onTap: () {
showModalBottomSheet(
context: context,
builder: (context) =>
// THIS DOESN'T HAVE THE RIGHT CONTEXT (PROVIDER NOT FOUND)
makeChild(showUI: true),
);
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
const Divider(height: 24),
Center(
child: IgnorePointer(
// THIS RENDERS FINE AND HAS THE CORRECT CONTEXT
child: FittedBox(child: makeChild(showUI: false)))),
],
),
),
);
}
}
In other words, the function directly inside the widget tree generates a widget that can access my Provider, but the same function that's in the showModalBottomSheet
generates one that can't. Why?