Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 주기억장치
- 문제해결 단계
- 원형 연결 구조 연결된 큐
- 논리 연산
- l-value참조자
- C언어 스택 연산
- 백준 파이썬
- 입출력 관리자
- const l-value참조자
- 회전 및 자리 이동 연산
- 프로그래머스 배열만들기4
- C언어 계산기 프로그램
- 알고리즘 조건
- string유형
- 프로그래머스 푸드 파이트 대회
- IPv4 주소체계
- 값/참조/주소에 의한 전달
- c언어 괄호검사
- 범위 기반 for문
- LAN의 분류
- 운영체제 기능
- 유형 변환
- 네트워크 결합
- auto 키워드
- 괄호 검사 프로그램
- 문자형 배열
- getline()함수
- r-value참조자
- C언어 덱
- const화
Archives
- Today
- Total
chyam
[flutter] - 이미지 자동으로 넘기기(PageController) 본문
프로젝트를 진행하며 사진들이 순서대로 자동으로 넘겨지거나 직접 넘길 수 있도록 하고자 하였습니다.
먼저 사용할 이미지들을 assets/images 내에 저장해주고, pubspec.yaml도 아래와같이 해준 뒤 Pub get을 해줍니다.
flutter:
...
assets:
- assets/images/
class _HomeScreenState extends State<HomeScreen> {
Timer? timer;
final List<String> _Examples = [
'assets/images/cat1.jpg',
'assets/images/cat2.jpg',
'assets/images/cat3.jpg',
];
// 이미지 슬라이더를 제어할 컨트롤러
final int _initialPage = 1002;
late PageController _pageController;
int _currentPage = 0;
currentPage는 0으로 설정해줍니다.
@override
void initState() {
super.initState();
// 시작 페이지 지정
_pageController = PageController(initialPage: _initialPage);
// 처음 켰을 때 점 위치를 0번으로 맞춤
_currentPage = _initialPage % _mosaicExamples.length;
timer = Timer.periodic(const Duration(seconds: 3), (timer) { // 3초타이머
if (_pageController.hasClients) {
// 다음 페이지로 그냥 1만 더해서 이동하면 됨
_pageController.nextPage(
duration: const Duration(milliseconds: 400),
curve: Curves.ease,
);
}
});
}
@override
void dispose() { // 위젯 죽을때
_pageController.dispose(); // 리소스 해제
if (timer!=null){
timer!.cancel();
}
super.dispose();
}
3초마다 사진이 옆으로 이동하게됩니다.
initState에서는 타이머 객체 생성 + 페이지 넘김
dispose에서는 리소스를 해제해줍니다.
@override
Widget build(BuildContext context) {
return Scaffold(
...
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center, // 모든 요소를 상단 중앙 정렬
children: [
const SizedBox(height: 20),
SizedBox(
height: 300,
child: PageView.builder(
controller: _pageController,
onPageChanged: (int page) {
setState(() {
// 현재 점의 위치는 '페이지 번호 % 사진개수'로 계산
_currentPage = page % _Examples.length;
});
},
itemBuilder: (context, index) {
// 실제 사진도 'index % 사진개수'로 가져옴
final imageIndex = index % _Examples.length;
return Container(
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(color: Colors.black12),
),
child: Image.asset(
_Examples[imageIndex], // 해당사진을 넣음
fit: BoxFit.cover,
),
);
},
),
),
// 자동으로 넘어가는 듯한 시각적 묘사를 위한 페이지 인디케이터
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(
_mosaicExamples.length,
(index) => Container(
margin: const EdgeInsets.symmetric(horizontal: 4),
width: _currentPage == index ? 10 : 6, // 선택됐으면 좀더 길게
height: 6,
decoration: BoxDecoration(
color: _currentPage == index ? Colors.black : Colors.grey.shade400, // 선택됐으면 검은색으로
borderRadius: BorderRadius.circular(3),
),
),
),
),
결과는 아래와 같습니다!
0
'Android' 카테고리의 다른 글
| [GeoCoding] 주소를 위/경도 변환하기(Geoservice) (2) | 2026.06.08 |
|---|---|
| [Android/Kotlin] - 사용자 참여형 장소 제보 및 승인 시스템(Firebase) (1) | 2026.06.01 |
| [Android] - 레이아웃이 상태바와 겹칠 때 (fitsSystemWindows) (0) | 2026.03.23 |
| [Android] - Naver Map API 연동하기 (0) | 2026.03.23 |
