infinite+carouSel function_2
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(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({ | |
super.key, | |
}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final ScrollController _c = ScrollController(); | |
final List<int> _itemList = []; | |
var _carouSelFlg = true; | |
final _itemCount = 10; | |
var _displayCount = 0.0; | |
var _itemData = 0.0; | |
var _carouSelCount = 0.0; | |
/// Used for swipe detection. right or left | |
var _pixels = 0.0; | |
var count = 0.5; | |
@override | |
void initState() { | |
super.initState(); | |
for (var i = 0; i < _itemCount; i++) { | |
_itemList.add(i); | |
} | |
var displayTextValue = 0.0; | |
_itemData = _itemList.length > 5 | |
? _itemList[_itemList.length - 3] * count | |
: _itemList[_itemList.length - 2] * count; | |
_displayCount = _itemList[_itemList.length - 4].toDouble(); | |
print(_itemData); | |
print(_displayCount); | |
_c.addListener(() { | |
setState(() { | |
var width = MediaQuery.of(context).size.width / 2; | |
var value = (MediaQuery.of(context).size.width - width) / 2; | |
if (_carouSelCount == _itemData) { | |
_carouSelCount = 0; | |
displayTextValue += _displayCount; | |
_infinityAnimation( | |
displayTextValue.toInt(), | |
0, | |
); | |
} else if (_carouSelCount < 0) { | |
_carouSelCount = _displayCount * count; | |
displayTextValue -= _displayCount; | |
_infinityAnimation( | |
displayTextValue.toInt(), | |
value * _displayCount, | |
); | |
} | |
_carouSelCheck(value); | |
}); | |
}); | |
} | |
Future<void> _infinityAnimation(int d1, double d2) async { | |
for (var i = 0; i < _itemCount; i++) { | |
_itemList[i] = i + d1; | |
} | |
_c.jumpTo(d2); | |
} | |
void _carouSelCheck(double value) { | |
if (_carouSelFlg) { | |
_carouSelFlg = false; | |
if (_c.position.pixels > _pixels) { | |
_carouSelCount += count; | |
} else { | |
_carouSelCount -= count; | |
} | |
_c | |
.animateTo((_carouSelCount * value * 2), | |
duration: const Duration(milliseconds: 200), curve: Curves.easeIn) | |
.then((value) => _carouSelFlg = true); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
var value = MediaQuery.of(context).size.width / 6; | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('ListView.builder'), | |
centerTitle: true, | |
), | |
body: Center( | |
child: SizedBox( | |
height: 200, | |
child: NotificationListener( | |
onNotification: (notificationInfo) { | |
if (notificationInfo is ScrollStartNotification) { | |
_pixels = _c.position.pixels; | |
} | |
return true; | |
}, | |
child: ListView.builder( | |
controller: _c, | |
scrollDirection: Axis.horizontal, | |
itemCount: _itemList.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Container( | |
alignment: Alignment.center, | |
margin: EdgeInsets.only(left: index == 0 ? 0 : value / 2), | |
height: value, | |
width: value, | |
color: _itemList[index].isOdd ? Colors.white : Colors.red, | |
child: TextButton.icon( | |
onPressed: () {}, | |
icon: const Icon(Icons.add), | |
label: Text('${_itemList[index]}'), | |
), | |
); | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment