I have a stateful widget class where I have a SingleChildScrollView which takes Column and a few widgets let's say w1, w2, w3, w4, and w5 all are scrollable what I want to achieve is when the user scrolls up the screen w1, w2, w4, w5 should behave as expected but w3 should stick when it reached to a fix position let say (screen height - 50).
Here is my code I am able to get the position and added a flag too "_isStuck", now I need to stick w3 widget when the flag turns true else it should scroll with the flow when the flag is false.
`import 'package:flutter/material.dart';
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
final GlobalKey _key = GlobalKey();
ScrollController _controller = ScrollController();
bool _isStuck = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
}
void _afterLayout(_) {
_controller.addListener(
() {
final RenderBox renderBox =
_key.currentContext!.findRenderObject() as RenderBox;
final Offset offset = renderBox.localToGlobal(Offset.zero);
final double startY = offset.dy;
if (startY <= 120) {
setState(() {
_isStuck = true;
});
} else {
setState(() {
_isStuck = false;
});
}
print("Check position: - $startY - $_isStuck");
},
);
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: _controller,
child: Column(
children: [
Container(
height: 400,
color: Colors.red,
child: const Text('w1'),
),
Container(
height: 400,
color: Colors.green,
child: const Text('w2'),
),
RepaintBoundary(
child: Container(
height: 100,
color: Colors.blue.shade400,
key: _key,
child: const Text('w3'),
),
),
Container(
height: 500,
color: Colors.yellow,
child: const Text('w4'),
),
Container(
height: 500,
color: Colors.orange,
child: const Text('w5'),
),
],
),
);
}
}