Last active
September 10, 2021 23:24
-
-
Save caseycrogers/c018ac774645be6619f9ac4de3c6d2dd to your computer and use it in GitHub Desktop.
Attempted RemoveListener Bug Repro
This file contains 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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
int? _selectedItem; | |
int _currPage = 0; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
body: Builder( | |
builder: (context) { | |
return SafeArea( | |
child: _currPage == 0 | |
? Navigator( | |
onPopPage: (route, result) { | |
if (!route.didPop(result)) { | |
return false; | |
} | |
_selectedItem = null; | |
return true; | |
}, | |
pages: [ | |
MaterialPage( | |
child: ListView( | |
children: [ | |
ScrollAwareWidget(), | |
...List.generate( | |
30, | |
(index) => TextButton( | |
onPressed: () { | |
setState(() { | |
_selectedItem = index; | |
}); | |
}, | |
child: Text(index.toString())), | |
), | |
], | |
), | |
), | |
if (_selectedItem != null) | |
MaterialPage( | |
child: Builder( | |
builder: (context) => Align( | |
alignment: Alignment.topLeft, | |
child: Row( | |
children: [ | |
IconButton( | |
onPressed: () => | |
Navigator.of(context).pop(), | |
icon: Icon(Icons.arrow_back), | |
), | |
Text('Selected Item: $_selectedItem'), | |
], | |
), | |
), | |
), | |
), | |
], | |
) | |
: Container(color: Colors.red), | |
); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon( | |
_currPage == 0 ? Icons.navigate_next : Icons.navigate_before), | |
onPressed: () { | |
setState(() { | |
if (_currPage == 0) { | |
_currPage = 1; | |
return; | |
} | |
_currPage = 0; | |
}); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class ScrollAwareWidget extends StatefulWidget { | |
ScrollAwareWidget({Key? key}) : super(key: key); | |
@override | |
_ScrollAwareWidgetState createState() => _ScrollAwareWidgetState(); | |
} | |
class _ScrollAwareWidgetState extends State<ScrollAwareWidget> { | |
bool initialized = false; | |
late ScrollPosition _position; | |
double _scrollProgress = 0; | |
@override | |
void didChangeDependencies() { | |
if (!initialized) { | |
print('Initializing ScrollAware'); | |
initialized = true; | |
_position = Scrollable.of(context)!.position; | |
_position.addListener(_onScroll); | |
} | |
super.didChangeDependencies(); | |
} | |
@override | |
void dispose() { | |
print('Disposing ScrollAware'); | |
_position.removeListener(_onScroll); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Text(_scrollProgress.toString(), style: TextStyle(fontSize: 36)); | |
} | |
void _onScroll() { | |
setState(() { | |
_scrollProgress = _position.pixels; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment