Skip to content

Instantly share code, notes, and snippets.

@caseycrogers
Created October 5, 2023 19:59
Show Gist options
  • Save caseycrogers/2d054d9efca1c329d0e644e524699b2e to your computer and use it in GitHub Desktop.
Save caseycrogers/2d054d9efca1c329d0e644e524699b2e to your computer and use it in GitHub Desktop.
testing ensure visible
void main() async {
FutureBuilder.debugRethrowError = true;
runApp(const _ScrollTestWidget());
}
class _ScrollTestWidget extends StatefulWidget {
const _ScrollTestWidget();
@override
State<_ScrollTestWidget> createState() => _ScrollTestWidgetState();
}
class _ScrollTestWidgetState extends State<_ScrollTestWidget> {
final FocusNode focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
Offstage(child: TextField(focusNode: focusNode)),
const Text('Here\'s some dead space above and below my widget.'),
const SizedBox(height: 400),
JumpToOnTap(focusNode: focusNode),
const SizedBox(height: 200),
],
),
),
),
);
}
}
class JumpToOnTap extends StatelessWidget {
const JumpToOnTap({required this.focusNode});
final FocusNode focusNode;
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () async {
final ScrollableState scrollable = Scrollable.of(context);
focusNode.requestFocus();
await Future.delayed(const Duration(milliseconds: 800));
// Ignore that this is unsafe for now.
// ignore: use_build_context_synchronously
final RenderBox renderBox = context.findRenderObject() as RenderBox;
await scrollable.position.ensureVisible(renderBox);
},
child: const Text('Tap me to open keyboard'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment