The code below functions as expected. Simply put, an overlay appears on the screen when the user presses the card and is removed when the user releases the press.
However, when I uncomment out the bit of code where I add a bloc event (it simply adds a timestamp to a variable in bloc's state) to the onLongPressStart gesture, I get the following error when I use the onLongPressEnd gesture:
_AssertionError ('package:flutter/src/widgets/overlay.dart': Failed assertion: line 147 pos 12: '_overlay != null': is not true.)
Here is the code. I create a ListView...
...
return CupertinoScrollbar(
child: ListView(
primary: false,
children: [
for (final entry in state.filteredEntries)
TodaysReviewListTile(
entry: entry,
onTap: () {
Navigator.of(context).push(
EditEntryPage.route(initialEntry: entry),
);
...
And then I build each card...
class TodaysReviewListTile extends StatelessWidget {
const TodaysReviewListTile({
Key? key,
required this.entry,
this.onTap,
}) : super(key: key);
final Entry entry;
final VoidCallback? onTap;
@override
Widget build(BuildContext context) {
final _entryTitle = entry.title;
final _entrySource = entry.source;
final _entryNotes = entry.notes;
final _entryUrl = entry.relatedUrl;
final _overlayState = Overlay.of(context);
final _overlayEntry = FocusAndIsolate(_entryTitle, _entryNotes);
return Padding(
padding: const EdgeInsets.all(12),
child: GestureDetector(
onLongPressStart: (_) {
_overlayState?.insert(_overlayEntry);
// context
// .read<TodaysReviewBloc>()
// .add(TodaysReviewFocusedLearningStart(entry));
},
onLongPressEnd: (_) {
_overlayEntry.remove();
},
child: Card(
elevation: 4,
child: Column(
children: [
ListTile(
...
The FocusAndIsolate object extends OverlayEntry...
class FocusAndIsolate extends OverlayEntry {
FocusAndIsolate(
String title,
String notes,
) : super(
maintainState: true,
builder: (context) {
final mediaQuery = MediaQuery.of(context);
return Stack(
children: [
Opacity(
opacity: .5,
child: Container(
color: Colors.black,
),
),
Positioned(
top: mediaQuery.size.height * .1,
height: mediaQuery.size.height * .8,
left: mediaQuery.size.width * .1,
width: mediaQuery.size.width * .8,
child: BackdropFilter(
filter: ui.ImageFilter.blur(
sigmaX: 2,
sigmaY: 2,
),
child: Material(
type: MaterialType.transparency,...
Why is this occurring? How do I avoid the error?