Last active
February 2, 2024 00:29
-
-
Save hardy716/8c74bc3ee5db83691a84a97c362184d1 to your computer and use it in GitHub Desktop.
플러터챌린지 9일차 기본문제 - 신현호
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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
const MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: MyRandomImageSliderScreen(), | |
), | |
); | |
} | |
class MyRandomImageSliderScreen extends StatefulWidget { | |
const MyRandomImageSliderScreen({super.key}); | |
@override | |
State<MyRandomImageSliderScreen> createState() => _MyRandomImageSliderScreenState(); | |
} | |
class _MyRandomImageSliderScreenState extends State<MyRandomImageSliderScreen> { | |
List<String> imgSrcList = []; | |
int _imageId = 0; | |
void getRandomImgSrc() { | |
String imgSrc = 'https://picsum.photos/id/${Random().nextInt(1000) + 1}/200/200'; | |
imgSrcList.add(imgSrc); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
getRandomImgSrc(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
ThemeData themeData = Theme.of(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text( | |
'Click left and right', | |
style: themeData.textTheme.titleLarge?.copyWith(color: themeData.primaryColor), | |
), | |
), | |
body: SafeArea( | |
child: SingleChildScrollView( | |
physics: const NeverScrollableScrollPhysics(), | |
child: Column( | |
children: [ | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
children: [ | |
ElevatedButton( | |
onPressed: () { | |
if (_imageId > 0) { | |
setState(() { | |
_imageId--; | |
}); | |
} | |
}, | |
child: const Icon(Icons.arrow_circle_left), | |
), | |
ElevatedButton( | |
onPressed: () { | |
getRandomImgSrc(); | |
setState(() { | |
_imageId++; | |
}); | |
}, | |
child: const Icon(Icons.arrow_circle_right), | |
) | |
], | |
), | |
SizedBox( | |
width: double.infinity, | |
child: Image.network( | |
imgSrcList[_imageId], | |
fit: BoxFit.cover, | |
frameBuilder: (context, child, frame, wasSynchronouslyLoaded) { | |
if (wasSynchronouslyLoaded) { | |
return child; | |
} | |
return frame != null ? child : const Center(child: Text('loading...')); | |
}, | |
/* 로딩 진행 상태에 따라 변하지 않는 단순한 플레이스홀더를 표시하는 경우에는 frameBuilder를 사용하는 것이 더 효율적 | |
* https://api.flutter.dev/flutter/widgets/Image/loadingBuilder.html | |
loadingBuilder: (context, child, loadingProgress) { | |
if (loadingProgress == null) return child; | |
return const Center( | |
child: Text('waiting...'), | |
); | |
}, | |
*/ | |
errorBuilder: (context, exception, stackTrace) { | |
return const Center( | |
child: Text('Oops! Something went wrong'), | |
); | |
}, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment