Last active
July 27, 2023 14:33
-
-
Save ChangJoo-Park/637b216c30d3f8eb7e65bdc37fbd7eba to your computer and use it in GitHub Desktop.
무한 스크롤되는 캐러셀 예제 https://dartpad.dev/?id=637b216c30d3f8eb7e65bdc37fbd7eba
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'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
home: CarouselScreen(), | |
); | |
} | |
} | |
class CarouselScreen extends StatefulWidget { | |
const CarouselScreen({super.key}); | |
@override | |
// ignore: library_private_types_in_public_api | |
_CarouselScreenState createState() => _CarouselScreenState(); | |
} | |
class _CarouselScreenState extends State<CarouselScreen> { | |
final List<String> carouselItems = [ | |
'Item 1', | |
'Item 2', | |
'Item 3', | |
'Item 4', | |
'Item 5', | |
]; | |
@override | |
void initState() { | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('무한스크롤'), | |
), | |
body: Center( | |
child: SizedBox( | |
height: 200, | |
child: InfiniteCarousel( | |
itemBuilder: (context, index) { | |
return Container( | |
margin: const EdgeInsets.symmetric(horizontal: 8), | |
color: Colors.blue, | |
child: const Center( | |
child: Text( | |
"Hello World", | |
style: TextStyle(fontSize: 24, color: Colors.white), | |
), | |
), | |
); | |
}, | |
itemCount: carouselItems.length, | |
onPageChanged: (int page) {}, | |
), | |
), | |
), | |
); | |
} | |
} | |
class InfiniteCarousel extends StatefulWidget { | |
const InfiniteCarousel({ | |
super.key, | |
required this.itemBuilder, | |
required this.itemCount, | |
this.onPageChanged, | |
}); | |
final Widget Function(BuildContext context, int index) itemBuilder; | |
final int itemCount; | |
final void Function(int page)? onPageChanged; | |
@override | |
State<InfiniteCarousel> createState() => _InfiniteCarouselState(); | |
} | |
class _InfiniteCarouselState extends State<InfiniteCarousel> { | |
late PageController _pageController; | |
int _currentPage = 0; | |
@override | |
void initState() { | |
super.initState(); | |
_pageController = PageController( | |
initialPage: _currentPage + widget.itemCount * 1000, | |
viewportFraction: 1, | |
); | |
_pageController.addListener(() { | |
setState(() { | |
_currentPage = (_pageController.page?.round() ?? 1) % widget.itemCount; | |
}); | |
widget.onPageChanged?.call(_currentPage); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return PageView.builder( | |
controller: _pageController, | |
itemBuilder: widget.itemBuilder, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment