iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
Mobile Development

Flutter with GetX, loading*175%歷程 系列 第 5

[Day05] Flutter with GetX carousel slider 水平輪播

  • 分享至 

  • xImage
  •  

Carousel slider

輪播動畫 原生要處理的話印象中是PageView

先建立一個widget class, 將package的封裝一次
construct傳入將要顯示的資料與點擊事件回傳

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'dart:math';

class CarouselSlide extends StatelessWidget {
  CarouselSlide({Key? key, required this.dataList, required this.didSelected})
      : super(key: key);
  final List dataList;
  final Function(int index) didSelected;

  @override
  Widget build(BuildContext context) {
    return CarouselSlider(
      options: CarouselOptions(
        height: MediaQuery.of(context).size.width * 0.86 * 9 / 16,
        aspectRatio: 16 / 9,
        enableInfiniteScroll: true,
        autoPlay: true,
        autoPlayInterval: const Duration(seconds: 2),
      ),
      items: dataList.map((element) {
        final index = dataList.indexOf(element);
        return GestureDetector(
          onTap: () => didSelected(index),
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 5.0),
            decoration: BoxDecoration(color: Colors.transparent),
            child: Stack(
              alignment: Alignment.center,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: Container(
                      color: Colors.primaries[
                          Random().nextInt(Colors.primaries.length)]),
                ),
                Positioned(
                  child: Text(
                    "$index",
                    style: TextStyle(fontSize: 30, color: Colors.white),
                  ),
                ),
              ],
            ),
          ),
        );
      }).toList(),
    );
  }
}

CarouselOptions內的屬性設定 
autoPlay:
是否自動換下一張, autoPlayInterval:換下一張間格幾秒
enableInfiniteScroll:
是否無限輪播,scrollDirection: 滑動方向(預設水平方向)

實際使用的頁面, 呼叫剛封裝的class

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:it_home/component/CarouselSlide.dart';
import 'package:it_home/pages/carousel_slider/CarouselSlidePageController.dart';

class CarouselSlidePage extends GetView<CarouselSlidePageController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('SliderListPage')),
      body: SafeArea(
        child: Center(
          child: CarouselSlide(
            dataList: controller.dataList,
            didSelected: (int index) {
              print("didTapped $index");
            },
          ),
        ),
      ),
    );
  }
}

因為有用到GetxController這邊也把程式碼一併放在這邊
生成一個List用於資料顯示

import 'package:get/get.dart';

class CarouselSlidePageController extends GetxController {
  final _dataList = [].obs;
  get dataList => this._dataList;

  @override
  void onInit() {
    _dataList.assignAll(Iterable<int>.generate(10).toList());
    super.onInit();
  }
}

最後畫面如下
https://miro.medium.com/max/404/1*Ma6vOhPJE9BV9C8jtUJ0EA.gif

本篇的GitHub source code

下一篇將為大家介紹shared preferences


上一篇
[Day04] Flutter with GetX Lottie animations
下一篇
[Day06] Flutter with GetX shared_preference
系列文
Flutter with GetX, loading*175%歷程 30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言