Created
February 19, 2019 00:07
-
-
Save marcusedu/23554cd5efe0b53e3535dd26f03960cc to your computer and use it in GitHub Desktop.
Simple PageIndicator
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 'package:flutter/material.dart'; | |
class AppPageIndicator extends StatefulWidget { | |
final PageController controller; | |
final int numItems; | |
final Color activeColor; | |
final Color inactiveColor; | |
final double dotSize; | |
final bool fillDots; | |
final EdgeInsets padding; | |
final bool shadow; | |
const AppPageIndicator(this.controller, | |
{Key key, | |
this.numItems = 0, | |
this.activeColor, | |
this.inactiveColor, | |
this.dotSize = 8, | |
this.fillDots = false, | |
this.padding, | |
this.shadow = true}) | |
: super(key: key); | |
@override | |
_AppPageIndicatorState createState() => _AppPageIndicatorState(); | |
} | |
class _AppPageIndicatorState extends State<AppPageIndicator> { | |
int _actualPage = 0; | |
@override | |
void initState() { | |
super.initState(); | |
widget.controller.removeListener(_pageChange); | |
widget.controller.addListener(_pageChange); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: widget.padding, | |
height: 8 + widget.dotSize, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: _getDots(widget.numItems), | |
), | |
); | |
} | |
_pageChange() { | |
setState(() { | |
_actualPage = widget.controller.page.round(); | |
}); | |
} | |
Widget _dot(Color color, {double size = 8.0}) => Container( | |
height: size, | |
width: size, | |
margin: EdgeInsets.all(4.0), | |
decoration: ShapeDecoration( | |
shape: CircleBorder(), | |
color: color, | |
shadows: widget.shadow | |
? [ | |
BoxShadow( | |
color: Colors.black54, | |
offset: Offset(0, 1.5), | |
blurRadius: 1.5) | |
] | |
: []), | |
); | |
List<Widget> _getDots(int numItems) { | |
var list = <Widget>[]; | |
for (var i = 0; i < numItems; i++) { | |
list.add(_dot( | |
(widget.fillDots ? i <= _actualPage : i == _actualPage) | |
? widget.activeColor | |
: widget.inactiveColor, | |
size: (widget.fillDots ? i <= _actualPage : i == _actualPage) | |
? widget.dotSize | |
: widget.dotSize * 0.85)); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment