dart 如何创建一个有三个图像的imageslider,并逐个滑动图像

evrscar2  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(139)


问题是我不能一个一个地滑动图像并显示三个图像。上图是我努力实现的目标。
在一个滑块中显示三个图像,然后逐个滑动到下一个,同时仍然显示三个图像

上面的图像鞋我的进步,它的外观是好的,但当我滑动它,它幻灯片三个图像在同一时间,下面是我的代码。

  1. Column(
  2. children: [
  3. Row(
  4. crossAxisAlignment: CrossAxisAlignment.center,
  5. mainAxisAlignment: MainAxisAlignment.center,
  6. children: [
  7. Container(
  8. width: 50,
  9. height: 400,
  10. child: MaterialButton(
  11. onPressed: () =>
  12. buttonCarouselController.previousPage(
  13. duration: const Duration(milliseconds: 300),
  14. curve: Curves.linear),
  15. child: const Icon(
  16. size: 45.0,
  17. Icons.arrow_back_ios,
  18. color: Color.fromARGB(255, 98, 124, 133)),
  19. ),
  20. ),
  21. Container(
  22. width: widthDevice - 120,
  23. height: 400,
  24. child: CarouselSlider.builder(
  25. itemCount: imageCount,
  26. options: CarouselOptions(
  27. autoPlay: true,
  28. // aspectRatio: 3.0,
  29. viewportFraction: 1,
  30. ),
  31. carouselController: buttonCarouselController,
  32. itemBuilder: (context, index, realIdx) {
  33. final int first = index * 2;
  34. final int second =
  35. index <= (imageList.length / 2).ceil() - 1
  36. ? first + 1
  37. : 0;
  38. final int third =
  39. index <= (imageList.length / 3).ceil() - 1
  40. ? first + 2
  41. : 0;
  42. return Column(
  43. crossAxisAlignment: CrossAxisAlignment.center,
  44. mainAxisAlignment: MainAxisAlignment.center,
  45. children: [
  46. Row(
  47. crossAxisAlignment:
  48. CrossAxisAlignment.center,
  49. mainAxisAlignment: MainAxisAlignment.center,
  50. children: [
  51. Column(
  52. children: [
  53. Container(
  54. width: widthDevice / 5,
  55. height: widthDevice / 5,
  56. decoration: BoxDecoration(
  57. image: DecorationImage(
  58. image: AssetImage(
  59. imageList[first]
  60. ['image']),
  61. fit: BoxFit.cover)),
  62. ),
  63. Container(
  64. width: widthDevice / 5,
  65. child: Padding(
  66. padding:
  67. const EdgeInsets.all(8.0),
  68. child: Text(
  69. imageList[first]
  70. ['description'],
  71. style: GoogleFonts.montserrat(
  72. textStyle: TextStyle(
  73. fontSize: 14,
  74. color: Color.fromARGB(
  75. 255, 51, 51, 51),
  76. ),
  77. )),
  78. ),
  79. ),
  80. ],
  81. ),
  82. SizedBox(
  83. width: 35,
  84. ),
  85. Column(
  86. children: [
  87. Container(
  88. width: widthDevice / 5,
  89. height: widthDevice / 5,
  90. decoration: BoxDecoration(
  91. image: DecorationImage(
  92. image: AssetImage(
  93. imageList[second]
  94. ['image']),
  95. fit: BoxFit.cover)),
  96. ),
  97. Container(
  98. width: widthDevice / 5,
  99. child: Padding(
  100. padding:
  101. const EdgeInsets.all(8.0),
  102. child: Text(
  103. imageList[second]
  104. ['description'],
  105. style: GoogleFonts.montserrat(
  106. textStyle: TextStyle(
  107. fontSize: 14,
  108. color: Color.fromARGB(
  109. 255, 51, 51, 51),
  110. ),
  111. )),
  112. ),
  113. ),
  114. ],
  115. ),
  116. SizedBox(
  117. width: 35,
  118. ),
  119. Column(
  120. children: [
  121. Container(
  122. width: widthDevice / 5,
  123. height: widthDevice / 5,
  124. decoration: BoxDecoration(
  125. image: DecorationImage(
  126. image: AssetImage(
  127. imageList[third]
  128. ['image']),
  129. fit: BoxFit.cover)),
  130. ),
  131. Container(
  132. width: widthDevice / 5,
  133. child: Padding(
  134. padding:
  135. const EdgeInsets.all(8.0),
  136. child: Text(
  137. imageList[third]
  138. ['description'],
  139. style: GoogleFonts.montserrat(
  140. textStyle: TextStyle(
  141. fontSize: 14,
  142. color: Color.fromARGB(
  143. 255, 51, 51, 51),
  144. ),
  145. )),
  146. ),
  147. ),
  148. ],
  149. ),
  150. ],
  151. ),
  152. ],
  153. );
  154. },
  155. )),
  156. Container(
  157. width: 50,
  158. height: 400,
  159. child: MaterialButton(
  160. onPressed: () => buttonCarouselController.nextPage(
  161. duration: const Duration(milliseconds: 300),
  162. curve: Curves.linear),
  163. child: const Icon(
  164. size: 45.0,
  165. Icons.arrow_forward_ios,
  166. color: Color.fromARGB(255, 98, 124, 133)),
  167. ),
  168. ),
  169. ],
  170. ),
  171. Row(
  172. mainAxisAlignment: MainAxisAlignment.center,
  173. children: imageList.asMap().entries.map((entry) {
  174. return GestureDetector(
  175. onTap: () => _controller.animateToPage(entry.key),
  176. child: Container(
  177. width: 55.0,
  178. height: 8.0,
  179. margin: EdgeInsets.symmetric(
  180. vertical: 8.0, horizontal: 4.0),
  181. decoration: BoxDecoration(
  182. shape: BoxShape.circle,
  183. color: (Theme.of(context).brightness ==
  184. Brightness.dark
  185. ? Color.fromARGB(255, 98, 124, 133)
  186. : const Color.fromARGB(
  187. 255, 252, 200, 178))
  188. .withOpacity(
  189. _current == entry.key ? 0.9 : 0.4)),
  190. ),
  191. );
  192. }).toList(),
  193. ),
  194. ],
  195. ))
nnt7mjpx

nnt7mjpx1#

要幻灯片图像并排,你可以使用下面的代码为您的要求

  1. import 'package:carousel_slider/carousel_slider.dart';
  2. import 'package:flutter/material.dart';
  3. final List<String> imgList = [
  4. 'https://images.unsplash.com/photo-1520342868574-5fa3804e551c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6ff92caffcdd63681a35134a6770ed3b&auto=format&fit=crop&w=1951&q=80',
  5. 'https://images.unsplash.com/photo-1522205408450-add114ad53fe?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=368f45b0888aeb0b7b08e3a1084d3ede&auto=format&fit=crop&w=1950&q=80',
  6. 'https://images.unsplash.com/photo-1519125323398-675f0ddb6308?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=94a1e718d89ca60a6337a6008341ca50&auto=format&fit=crop&w=1950&q=80',
  7. 'https://images.unsplash.com/photo-1523205771623-e0faa4d2813d?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=89719a0d55dd05e2deae4120227e6efc&auto=format&fit=crop&w=1953&q=80',
  8. 'https://images.unsplash.com/photo-1508704019882-f9cf40e475b4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8c6e5e3aba713b17aa1fe71ab4f0ae5b&auto=format&fit=crop&w=1352&q=80',
  9. 'https://images.unsplash.com/photo-1519985176271-adb1088fa94c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=a0c8d632e977f94e5d312d9893258f59&auto=format&fit=crop&w=1355&q=80'
  10. ];
  11. void main() => runApp(CarouselDemo());
  12. final themeMode = ValueNotifier(2);
  13. class CarouselDemo extends StatelessWidget {
  14. @override
  15. Widget build(BuildContext context) {
  16. return ValueListenableBuilder(
  17. builder: (context, value, g) {
  18. return MaterialApp(
  19. initialRoute: '/',
  20. darkTheme: ThemeData.dark(),
  21. themeMode: ThemeMode.values.toList()[value as int],
  22. debugShowCheckedModeBanner: false,
  23. routes: {
  24. '/': (ctx) => CarouselDemoHome(),
  25. '/basic': (ctx) => BasicDemo(),
  26. '/nocenter': (ctx) => NoCenterDemo(),
  27. '/image': (ctx) => ImageSliderDemo(),
  28. '/complicated': (ctx) => ComplicatedImageDemo(),
  29. '/enlarge': (ctx) => EnlargeStrategyDemo(),
  30. '/manual': (ctx) => ManuallyControlledSlider(),
  31. '/noloop': (ctx) => NoonLoopingDemo(),
  32. '/vertical': (ctx) => VerticalSliderDemo(),
  33. '/fullscreen': (ctx) => FullscreenSliderDemo(),
  34. '/ondemand': (ctx) => OnDemandCarouselDemo(),
  35. '/indicator': (ctx) => CarouselWithIndicatorDemo(),
  36. '/prefetch': (ctx) => PrefetchImageDemo(),
  37. '/reason': (ctx) => CarouselChangeReasonDemo(),
  38. '/position': (ctx) => KeepPageviewPositionDemo(),
  39. '/multiple': (ctx) => MultipleItemDemo(),
  40. '/zoom': (ctx) => EnlargeStrategyZoomDemo(),
  41. },
  42. );
  43. },
  44. valueListenable: themeMode,
  45. );
  46. }
  47. }
  48. class DemoItem extends StatelessWidget {
  49. final String title;
  50. final String route;
  51. DemoItem(this.title, this.route);
  52. @override
  53. Widget build(BuildContext context) {
  54. return ListTile(
  55. title: Text(title),
  56. onTap: () {
  57. Navigator.pushNamed(context, route);
  58. },
  59. );
  60. }
  61. }
  62. class CarouselDemoHome extends StatelessWidget {
  63. @override
  64. Widget build(BuildContext context) {
  65. return Scaffold(
  66. appBar: AppBar(
  67. title: Text('Carousel demo'),
  68. actions: [
  69. IconButton(
  70. icon: Icon(Icons.nightlight_round),
  71. onPressed: () {
  72. themeMode.value = themeMode.value == 1 ? 2 : 1;
  73. })
  74. ],
  75. ),
  76. body: ListView(
  77. children: <Widget>[
  78. DemoItem('Basic demo', '/basic'),
  79. DemoItem('No center mode demo', '/nocenter'),
  80. DemoItem('Image carousel slider', '/image'),
  81. DemoItem('More complicated image slider', '/complicated'),
  82. DemoItem('Enlarge strategy demo slider', '/enlarge'),
  83. DemoItem('Manually controlled slider', '/manual'),
  84. DemoItem('Noon-looping carousel slider', '/noloop'),
  85. DemoItem('Vertical carousel slider', '/vertical'),
  86. DemoItem('Fullscreen carousel slider', '/fullscreen'),
  87. DemoItem('Carousel with indicator controller demo', '/indicator'),
  88. DemoItem('On-demand carousel slider', '/ondemand'),
  89. DemoItem('Image carousel slider with prefetch demo', '/prefetch'),
  90. DemoItem('Carousel change reason demo', '/reason'),
  91. DemoItem('Keep pageview position demo', '/position'),
  92. DemoItem('Multiple item in one screen demo', '/multiple'),
  93. DemoItem('Enlarge strategy: zoom demo', '/zoom'),
  94. ],
  95. ),
  96. );
  97. }
  98. }
  99. class BasicDemo extends StatelessWidget {
  100. @override
  101. Widget build(BuildContext context) {
  102. List<int> list = [1, 2, 3, 4, 5];
  103. return Scaffold(
  104. appBar: AppBar(title: Text('Basic demo')),
  105. body: Container(
  106. child: CarouselSlider(
  107. options: CarouselOptions(),
  108. items: list
  109. .map((item) => Container(
  110. child: Center(child: Text(item.toString())),
  111. color: Colors.green,
  112. ))
  113. .toList(),
  114. )),
  115. );
  116. }
  117. }
  118. class NoCenterDemo extends StatelessWidget {
  119. @override
  120. Widget build(BuildContext context) {
  121. List<int> list = [1, 2, 3, 4, 5];
  122. return Scaffold(
  123. appBar: AppBar(title: Text('Basic demo')),
  124. body: Container(
  125. child: CarouselSlider(
  126. options: CarouselOptions(
  127. disableCenter: true,
  128. ),
  129. items: list
  130. .map((item) => Container(
  131. child: Text(item.toString()),
  132. color: Colors.green,
  133. ))
  134. .toList(),
  135. )),
  136. );
  137. }
  138. }
  139. class ImageSliderDemo extends StatelessWidget {
  140. @override
  141. Widget build(BuildContext context) {
  142. return Scaffold(
  143. appBar: AppBar(title: Text('Image slider demo')),
  144. body: Container(
  145. child: CarouselSlider(
  146. options: CarouselOptions(),
  147. items: imgList
  148. .map((item) => Container(
  149. child: Center(
  150. child:
  151. Image.network(item, fit: BoxFit.cover, width: 1000)),
  152. ))
  153. .toList(),
  154. )),
  155. );
  156. }
  157. }
  158. final List<Widget> imageSliders = imgList
  159. .map((item) => Container(
  160. child: Container(
  161. margin: EdgeInsets.all(5.0),
  162. child: ClipRRect(
  163. borderRadius: BorderRadius.all(Radius.circular(5.0)),
  164. child: Stack(
  165. children: <Widget>[
  166. Image.network(item, fit: BoxFit.cover, width: 1000.0),
  167. Positioned(
  168. bottom: 0.0,
  169. left: 0.0,
  170. right: 0.0,
  171. child: Container(
  172. decoration: BoxDecoration(
  173. gradient: LinearGradient(
  174. colors: [
  175. Color.fromARGB(200, 0, 0, 0),
  176. Color.fromARGB(0, 0, 0, 0)
  177. ],
  178. begin: Alignment.bottomCenter,
  179. end: Alignment.topCenter,
  180. ),
  181. ),
  182. padding: EdgeInsets.symmetric(
  183. vertical: 10.0, horizontal: 20.0),
  184. child: Text(
  185. 'No. ${imgList.indexOf(item)} image',
  186. style: TextStyle(
  187. color: Colors.white,
  188. fontSize: 20.0,
  189. fontWeight: FontWeight.bold,
  190. ),
  191. ),
  192. ),
  193. ),
  194. ],
  195. )),
  196. ),
  197. ))
  198. .toList();
  199. class ComplicatedImageDemo extends StatelessWidget {
  200. @override
  201. Widget build(BuildContext context) {
  202. return Scaffold(
  203. appBar: AppBar(title: Text('Complicated image slider demo')),
  204. body: Container(
  205. child: CarouselSlider(
  206. options: CarouselOptions(
  207. autoPlay: true,
  208. aspectRatio: 2.0,
  209. enlargeCenterPage: true,
  210. ),
  211. items: imageSliders,
  212. ),
  213. ),
  214. );
  215. }
  216. }
  217. class EnlargeStrategyDemo extends StatelessWidget {
  218. @override
  219. Widget build(BuildContext context) {
  220. return Scaffold(
  221. appBar: AppBar(title: Text('Complicated image slider demo')),
  222. body: Container(
  223. child: CarouselSlider(
  224. options: CarouselOptions(
  225. autoPlay: true,
  226. aspectRatio: 2.0,
  227. enlargeCenterPage: true,
  228. enlargeStrategy: CenterPageEnlargeStrategy.height,
  229. ),
  230. items: imageSliders,
  231. ),
  232. ),
  233. );
  234. }
  235. }
  236. class ManuallyControlledSlider extends StatefulWidget {
  237. @override
  238. State<StatefulWidget> createState() {
  239. return _ManuallyControlledSliderState();
  240. }
  241. }
  242. class _ManuallyControlledSliderState extends State<ManuallyControlledSlider> {
  243. final CarouselController _controller = CarouselController();
  244. @override
  245. void initState() {
  246. super.initState();
  247. }
  248. @override
  249. Widget build(BuildContext context) {
  250. return Scaffold(
  251. appBar: AppBar(title: Text('Manually controlled slider')),
  252. body: SingleChildScrollView(
  253. child: Column(
  254. children: <Widget>[
  255. CarouselSlider(
  256. items: imageSliders,
  257. options: CarouselOptions(enlargeCenterPage: true, height: 200),
  258. carouselController: _controller,
  259. ),
  260. Row(
  261. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  262. children: <Widget>[
  263. Flexible(
  264. child: ElevatedButton(
  265. onPressed: () => _controller.previousPage(),
  266. child: Text('←'),
  267. ),
  268. ),
  269. Flexible(
  270. child: ElevatedButton(
  271. onPressed: () => _controller.nextPage(),
  272. child: Text('→'),
  273. ),
  274. ),
  275. ...Iterable<int>.generate(imgList.length).map(
  276. (int pageIndex) => Flexible(
  277. child: ElevatedButton(
  278. onPressed: () => _controller.animateToPage(pageIndex),
  279. child: Text("$pageIndex"),
  280. ),
  281. ),
  282. ),
  283. ],
  284. )
  285. ],
  286. ),
  287. ));
  288. }
  289. }
  290. class NoonLoopingDemo extends StatelessWidget {
  291. @override
  292. Widget build(BuildContext context) {
  293. return Scaffold(
  294. appBar: AppBar(title: Text('Noon-looping carousel demo')),
  295. body: Container(
  296. child: CarouselSlider(
  297. options: CarouselOptions(
  298. aspectRatio: 2.0,
  299. enlargeCenterPage: true,
  300. enableInfiniteScroll: false,
  301. initialPage: 2,
  302. autoPlay: true,
  303. ),
  304. items: imageSliders,
  305. )),
  306. );
  307. }
  308. }
  309. class VerticalSliderDemo extends StatelessWidget {
  310. @override
  311. Widget build(BuildContext context) {
  312. return Scaffold(
  313. appBar: AppBar(title: Text('Vertical sliding carousel demo')),
  314. body: Container(
  315. child: CarouselSlider(
  316. options: CarouselOptions(
  317. aspectRatio: 2.0,
  318. enlargeCenterPage: true,
  319. scrollDirection: Axis.vertical,
  320. autoPlay: true,
  321. ),
  322. items: imageSliders,
  323. )),
  324. );
  325. }
  326. }
  327. class FullscreenSliderDemo extends StatelessWidget {
  328. @override
  329. Widget build(BuildContext context) {
  330. return Scaffold(
  331. appBar: AppBar(title: Text('Fullscreen sliding carousel demo')),
  332. body: Builder(
  333. builder: (context) {
  334. final double height = MediaQuery.of(context).size.height;
  335. return CarouselSlider(
  336. options: CarouselOptions(
  337. height: height,
  338. viewportFraction: 1.0,
  339. enlargeCenterPage: false,
  340. // autoPlay: false,
  341. ),
  342. items: imgList
  343. .map((item) => Container(
  344. child: Center(
  345. child: Image.network(
  346. item,
  347. fit: BoxFit.cover,
  348. height: height,
  349. )),
  350. ))
  351. .toList(),
  352. );
  353. },
  354. ),
  355. );
  356. }
  357. }
  358. class OnDemandCarouselDemo extends StatelessWidget {
  359. @override
  360. Widget build(BuildContext context) {
  361. return Scaffold(
  362. appBar: AppBar(title: Text('On-demand carousel demo')),
  363. body: Container(
  364. child: CarouselSlider.builder(
  365. itemCount: 100,
  366. options: CarouselOptions(
  367. aspectRatio: 2.0,
  368. enlargeCenterPage: true,
  369. autoPlay: true,
  370. ),
  371. itemBuilder: (ctx, index, realIdx) {
  372. return Container(
  373. child: Text(index.toString()),
  374. );
  375. },
  376. )),
  377. );
  378. }
  379. }
  380. class CarouselWithIndicatorDemo extends StatefulWidget {
  381. @override
  382. State<StatefulWidget> createState() {
  383. return _CarouselWithIndicatorState();
  384. }
  385. }
  386. class _CarouselWithIndicatorState extends State<CarouselWithIndicatorDemo> {
  387. int _current = 0;
  388. final CarouselController _controller = CarouselController();
  389. @override
  390. Widget build(BuildContext context) {
  391. return Scaffold(
  392. appBar: AppBar(title: Text('Carousel with indicator controller demo')),
  393. body: Column(children: [
  394. Expanded(
  395. child: CarouselSlider(
  396. items: imageSliders,
  397. carouselController: _controller,
  398. options: CarouselOptions(
  399. autoPlay: true,
  400. enlargeCenterPage: true,
  401. aspectRatio: 2.0,
  402. onPageChanged: (index, reason) {
  403. setState(() {
  404. _current = index;
  405. });
  406. }),
  407. ),
  408. ),
  409. Row(
  410. mainAxisAlignment: MainAxisAlignment.center,
  411. children: imgList.asMap().entries.map((entry) {
  412. return GestureDetector(
  413. onTap: () => _controller.animateToPage(entry.key),
  414. child: Container(
  415. width: 12.0,
  416. height: 12.0,
  417. margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
  418. decoration: BoxDecoration(
  419. shape: BoxShape.circle,
  420. color: (Theme.of(context).brightness == Brightness.dark
  421. ? Colors.white
  422. : Colors.black)
  423. .withOpacity(_current == entry.key ? 0.9 : 0.4)),
  424. ),
  425. );
  426. }).toList(),
  427. ),
  428. ]),
  429. );
  430. }
  431. }
  432. class PrefetchImageDemo extends StatefulWidget {
  433. @override
  434. State<StatefulWidget> createState() {
  435. return _PrefetchImageDemoState();
  436. }
  437. }
  438. class _PrefetchImageDemoState extends State<PrefetchImageDemo> {
  439. final List<String> images = [
  440. 'https://images.unsplash.com/photo-1586882829491-b81178aa622e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
  441. 'https://images.unsplash.com/photo-1586871608370-4adee64d1794?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2862&q=80',
  442. 'https://images.unsplash.com/photo-1586901533048-0e856dff2c0d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
  443. 'https://images.unsplash.com/photo-1586902279476-3244d8d18285?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80',
  444. 'https://images.unsplash.com/photo-1586943101559-4cdcf86a6f87?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1556&q=80',
  445. 'https://images.unsplash.com/photo-1586951144438-26d4e072b891?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
  446. 'https://images.unsplash.com/photo-1586953983027-d7508a64f4bb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1650&q=80',
  447. ];
  448. @override
  449. void initState() {
  450. WidgetsBinding.instance.addPostFrameCallback((_) {
  451. images.forEach((imageUrl) {
  452. precacheImage(NetworkImage(imageUrl), context);
  453. });
  454. });
  455. super.initState();
  456. }
  457. @override
  458. Widget build(BuildContext context) {
  459. return Scaffold(
  460. appBar: AppBar(title: Text('Prefetch image slider demo')),
  461. body: Container(
  462. child: CarouselSlider.builder(
  463. itemCount: images.length,
  464. options: CarouselOptions(
  465. autoPlay: true,
  466. aspectRatio: 2.0,
  467. enlargeCenterPage: true,
  468. ),
  469. itemBuilder: (context, index, realIdx) {
  470. return Container(
  471. child: Center(
  472. child: Image.network(images[index],
  473. fit: BoxFit.cover, width: 1000)),
  474. );
  475. },
  476. )),
  477. );
  478. }
  479. }
  480. class CarouselChangeReasonDemo extends StatefulWidget {
  481. @override
  482. State<StatefulWidget> createState() {
  483. return _CarouselChangeReasonDemoState();
  484. }
  485. }
  486. class _CarouselChangeReasonDemoState extends State<CarouselChangeReasonDemo> {
  487. String reason = '';
  488. final CarouselController _controller = CarouselController();
  489. void onPageChange(int index, CarouselPageChangedReason changeReason) {
  490. setState(() {
  491. reason = changeReason.toString();
  492. });
  493. }
  494. @override
  495. Widget build(BuildContext context) {
  496. return Scaffold(
  497. appBar: AppBar(title: Text('Change reason demo')),
  498. body: Column(
  499. children: <Widget>[
  500. Expanded(
  501. child: CarouselSlider(
  502. items: imageSliders,
  503. options: CarouselOptions(
  504. enlargeCenterPage: true,
  505. aspectRatio: 16 / 9,
  506. onPageChanged: onPageChange,
  507. autoPlay: true,
  508. ),
  509. carouselController: _controller,
  510. ),
  511. ),
  512. Row(
  513. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  514. children: <Widget>[
  515. Flexible(
  516. child: ElevatedButton(
  517. onPressed: () => _controller.previousPage(),
  518. child: Text('←'),
  519. ),
  520. ),
  521. Flexible(
  522. child: ElevatedButton(
  523. onPressed: () => _controller.nextPage(),
  524. child: Text('→'),
  525. ),
  526. ),
  527. ...Iterable<int>.generate(imgList.length).map(
  528. (int pageIndex) => Flexible(
  529. child: ElevatedButton(
  530. onPressed: () => _controller.animateToPage(pageIndex),
  531. child: Text("$pageIndex"),
  532. ),
  533. ),
  534. ),
  535. ],
  536. ),
  537. Center(
  538. child: Column(
  539. children: [
  540. Text('page change reason: '),
  541. Text(reason),
  542. ],
  543. ),
  544. )
  545. ],
  546. ));
  547. }
  548. }
  549. class KeepPageviewPositionDemo extends StatelessWidget {
  550. @override
  551. Widget build(BuildContext context) {
  552. return Scaffold(
  553. appBar: AppBar(title: Text('Keep pageview position demo')),
  554. body: ListView.builder(itemBuilder: (ctx, index) {
  555. if (index == 3) {
  556. return Container(
  557. child: CarouselSlider(
  558. options: CarouselOptions(
  559. aspectRatio: 2.0,
  560. enlargeCenterPage: true,
  561. pageViewKey: PageStorageKey<String>('carousel_slider'),
  562. ),
  563. items: imageSliders,
  564. ));
  565. } else {
  566. return Container(
  567. margin: EdgeInsets.symmetric(vertical: 20),
  568. color: Colors.blue,
  569. height: 200,
  570. child: Center(
  571. child: Text('other content'),
  572. ),
  573. );
  574. }
  575. }),
  576. );
  577. }
  578. }
  579. class MultipleItemDemo extends StatelessWidget {
  580. @override
  581. Widget build(BuildContext context) {
  582. return Scaffold(
  583. appBar: AppBar(title: Text('Multiple item in one slide demo')),
  584. body: Container(
  585. child: CarouselSlider.builder(
  586. options: CarouselOptions(
  587. aspectRatio: 2.0,
  588. enlargeCenterPage: false,
  589. viewportFraction: 1,
  590. ),
  591. itemCount: (imgList.length / 2).round(),
  592. itemBuilder: (context, index, realIdx) {
  593. final int first = index * 2;
  594. final int second = first + 1;
  595. return Row(
  596. children: [first, second].map((idx) {
  597. return Expanded(
  598. flex: 1,
  599. child: Container(
  600. margin: EdgeInsets.symmetric(horizontal: 10),
  601. child: Image.network(imgList[idx], fit: BoxFit.cover),
  602. ),
  603. );
  604. }).toList(),
  605. );
  606. },
  607. )),
  608. );
  609. }
  610. }
  611. class EnlargeStrategyZoomDemo extends StatelessWidget {
  612. @override
  613. Widget build(BuildContext context) {
  614. return Scaffold(
  615. appBar: AppBar(title: Text('enlarge strategy: zoom demo')),
  616. body: Container(
  617. child: CarouselSlider(
  618. options: CarouselOptions(
  619. aspectRatio: 2.0,
  620. enlargeCenterPage: true,
  621. enlargeStrategy: CenterPageEnlargeStrategy.zoom,
  622. enlargeFactor: 0.4,
  623. ),
  624. items: imageSliders,
  625. ),
  626. ),
  627. );
  628. }
  629. }

你可以搜索这个插件以获得更多的细节

  1. carousel_slider: ^4.2.1

它还提供自动播放功能,
希望对你有帮助!

展开查看全部

相关问题