Skip to content

Instantly share code, notes, and snippets.

@waelalameen
Created April 23, 2022 11:17
Show Gist options
  • Save waelalameen/4fbfc72207fcb3164bb1d071506376f7 to your computer and use it in GitHub Desktop.
Save waelalameen/4fbfc72207fcb3164bb1d071506376f7 to your computer and use it in GitHub Desktop.
Custom scroll demo
import 'package:flutter/material.dart';
const LIMIT = 200;
// You can manipulate this factor as much as you want.
const SCROLL_FACTOR = 0.3;
var offset = 60.2;
class Scroll extends StatefulWidget {
const Scroll({Key key}) : super(key: key);
@override
State<Scroll> createState() => _ScrollState();
}
class _ScrollState extends State<Scroll> {
var _controller = ScrollController(initialScrollOffset: 0.0);
@override
void initState() {
_controller.addListener(_scrollListener);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _customScroll(context),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
_scrollListener() {
setState(
() {
if (_controller.offset >= LIMIT) {
offset = SCROLL_FACTOR * _controller.offset;
} else {
offset = 60.2;
}
},
);
}
Widget _customScroll(context) {
return Stack(
children: [
Transform.translate(
offset: Offset(0, -offset),
child: Image.network(
'https://cdn.unenvironment.org/2022-03/field-ge4d2466da_1920.jpg',
alignment: Alignment.topCenter,
fit: BoxFit.fitHeight,
height: 400.0,
),
),
SingleChildScrollView(
controller: _controller,
child: Container(
margin: EdgeInsets.only(top: 300),
child: Column(
children: List.generate(
6,
(index) {
return Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 700.0,
child: Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Item No #${index + 1}',
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold),
),
),
),
),
);
},
),
),
),
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment