Flutter 仿iOS自定義UIPageControl組件

import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

class DWPageView extends StatefulWidget {

DWPageView({

// 寬度 必傳

this.width,

// 高度 必傳

this.height,

// 總頁(yè)數(shù) 必傳

this.numberOfPages,

// 當(dāng)前頁(yè)數(shù)

this.currentPage = 0,

// 未選顏色 默認(rèn)灰色

this.pageColor = Colors.grey,

// 選中顏色 默認(rèn)白色

this.tintColor = Colors.white,

// 間隔 默認(rèn)5

this.space = 5,

// 指示器寬度 默認(rèn)5

this.pageWidth = 5,

// 指示器高度 默認(rèn)5

this.pageHeight = 5,

// 選中指示器寬度 默認(rèn)5

this.currentPageWidth = 5,

// 選中指示器高度 默認(rèn)5

this.currentPageHeight = 5,

// 指示器圓角度 默認(rèn)2

this.radius = 2,

// 排序的方向 默認(rèn)橫著排序

this.scrollDirection = Axis.horizontal,

Key key,

}) : super( key: key);

final int numberOfPages;

final int currentPage;

final Color pageColor;

final Color tintColor;

final double space;

final double width;

final double height;

final double pageWidth;

final double pageHeight;

final double currentPageWidth;

final double currentPageHeight;

final double radius;

final Axis scrollDirection;

@override

DWPageViewState createState() {

return DWPageViewState();

}

}

class DWPageViewState extends State {

ScrollController _scrollController = new ScrollController();

int _currentPage;

@override

void initState() {

// TODO: implement initState

super.initState();

setState(() {

_currentPage = widget.currentPage;

});

}

void selectedIndex(int index) {

setState(() {

_currentPage = index;

});

}

@override

Widget build(BuildContext context) {

// TODO: implement build

return Container(

width: widget.width,

height: widget.height,

child: ListView.builder(

controller: _scrollController,

scrollDirection: widget.scrollDirection,

itemCount: widget.numberOfPages,

itemBuilder: ((context, index) {

if (index == _currentPage) {

return Center(

child: Container(

margin: new EdgeInsets.fromLTRB(0, 0, widget.scrollDirection == Axis.horizontal ? widget.space : 0, widget.scrollDirection != Axis.horizontal ? widget.space : 0),

width: widget.currentPageWidth,

height: widget.currentPageHeight,

decoration: BoxDecoration(

borderRadius: BorderRadius.all(Radius.circular(widget.radius)),

color: widget.tintColor,

),

),

);

}

return Center(

child: Container(

margin: new EdgeInsets.fromLTRB(0, 0, widget.scrollDirection == Axis.horizontal ? widget.space : 0, widget.scrollDirection != Axis.horizontal ? widget.space : 0),

width: widget.pageWidth,

height: widget.pageHeight,

decoration: BoxDecoration(

color: widget.pageColor,

borderRadius: BorderRadius.all(Radius.circular(widget.radius)),

),

),

);

}),

),

);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容