Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active August 17, 2022 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/627906e79ec5b16c2b0286a4607f216b to your computer and use it in GitHub Desktop.
Save DaisukeNagata/627906e79ec5b16c2b0286a4607f216b to your computer and use it in GitHub Desktop.
infinite scroll
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 = [];
final _displayCount = 4;
final _itemCount = 30;
double? offset = 0;
@override
void initState() {
super.initState();
for (var i = 0; i < _itemCount; i++) {
_itemList.add(i);
}
var displayValue = 0;
var displayTextValue = 0;
_c.addListener(() {
setState(() {
var value = (MediaQuery.of(context).size.width / _displayCount) / 2;
if (_c.position.pixels == _c.position.maxScrollExtent) {
offset = null;
displayValue += _displayCount;
displayTextValue += _itemList.length;
_infinityAnimation(
displayValue,
displayTextValue,
0,
);
} else if (_c.offset < -value && _itemList.first != 0) {
offset = null;
displayValue -= _displayCount;
displayTextValue -= _itemList.length;
_infinityAnimation(
displayValue,
displayTextValue,
_c.position.maxScrollExtent / 1.0001,
);
}
});
});
}
Future<void> _infinityAnimation(int d, int d2, double d3) async {
for (var i = 0; i < _itemCount; i++) {
_itemList[i] = i + d2 - d;
}
offset = d3;
_c.jumpTo(d3);
}
Future<double?> setValue() async {
return offset;
}
@override
Widget build(BuildContext context) {
var value = MediaQuery.of(context).size.width / _displayCount;
return Scaffold(
appBar: AppBar(
title: const Text('ListView.builder'),
centerTitle: true,
),
body: Center(
child: SizedBox(
height: 200,
child: ListView.builder(
controller: _c,
scrollDirection: Axis.horizontal,
itemCount: _itemList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
alignment: Alignment.center,
height: value,
width: value,
color: index.isOdd ? Colors.white : Colors.red,
child: FutureBuilder(
future: setValue(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return TextButton.icon(
onPressed: () {},
icon: const Icon(Icons.add),
label: Text('${_itemList[index]}'),
);
} else {
return Container();
}
},
),
);
},
),
),
),
);
}
}
@DaisukeNagata
Copy link
Author

DaisukeNagata commented Aug 17, 2022

スクリーンショット 2022-08-17 22 49 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment