Created
September 28, 2022 18:31
-
-
Save sorgfal/b6e01fe2a85d4224dff37ba6fd5afb40 to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
| import 'package:flutter/rendering.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: MyHome( | |
| now: DateTime.now(), | |
| ), | |
| ); | |
| } | |
| } | |
| class MyHome extends StatefulWidget { | |
| final DateTime now; | |
| const MyHome({super.key, required this.now}); | |
| @override | |
| _MyHomeState createState() => _MyHomeState(); | |
| } | |
| class _MyHomeState extends State<MyHome> { | |
| late ScrollController controller; | |
| late List<DateTime> items; | |
| @override | |
| void initState() { | |
| items = List.generate( | |
| 10, (index) => widget.now.subtract(Duration(days: index))); | |
| controller = ScrollController()..addListener(_scrollListener); | |
| super.initState(); | |
| } | |
| @override | |
| void dispose() { | |
| controller.removeListener(_scrollListener); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Scrollbar( | |
| controller: controller, | |
| child: ListView.builder( | |
| controller: controller, | |
| itemBuilder: (context, index) { | |
| return MyCard( | |
| d: items[index], | |
| ); | |
| }, | |
| itemCount: items.length, | |
| ), | |
| ), | |
| ); | |
| } | |
| void _scrollListener() { | |
| if (controller.position.userScrollDirection == ScrollDirection.forward && | |
| controller.position.pixels < 0) { | |
| items.insertAll(0, | |
| List.generate(1, (index) => items.first.subtract(Duration(days: 1)))); | |
| // сместиться на высоту карточки | |
| controller.jumpTo(90); | |
| setState(() {}); | |
| } | |
| if (controller.position.extentAfter < 500) { | |
| controller.jumpTo(-90); | |
| setState(() { | |
| items.add(items.last.add(Duration(days: 1))); | |
| }); | |
| } | |
| } | |
| } | |
| class MyCard extends StatelessWidget { | |
| final DateTime d; | |
| const MyCard({super.key, required this.d}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Container( | |
| color: Colors.amber[50], | |
| margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8), | |
| height: 100, | |
| child: Center(child: Text(d.toIso8601String().substring(0, 19))), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment