Skip to content

Instantly share code, notes, and snippets.

@preetjdp
Created February 21, 2020 07:26
Show Gist options
  • Select an option

  • Save preetjdp/3a61b9ee8e14654539b79aba0bc71ffb to your computer and use it in GitHub Desktop.

Select an option

Save preetjdp/3a61b9ee8e14654539b79aba0bc71ffb to your computer and use it in GitHub Desktop.
This is a demo to show of the DotsIndicator
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Page Indicator Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
PageController pageController = PageController();
@override
Widget build(BuildContext context) {
// double width = MediaQuery.of(context).size.width;
// double height = MediaQuery.of(context).size.height;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
alignment: Alignment.center,
children: <Widget>[
PageView.builder(
itemCount: 5,
controller: pageController,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: EdgeInsets.all(30),
child: Container(
height: 200,
width: 200,
color: Colors.red
));
},
),
Positioned(
bottom: 10,
child: Center(
child: DotsIndicator(
controller: pageController,
itemCount: 5,
color: Colors.black,
// dotSize: 2.5,
// maxZoom: 2,
// dotSpacing: 8,
),
),
),
],
)
);
}
}
class DotsIndicator extends AnimatedWidget {
DotsIndicator(
{this.controller,
this.itemCount,
this.onPageSelected,
this.color: Colors.white,
this.dotSize = 4.0,
this.maxZoom = 2,
this.dotSpacing = 18})
: super(listenable: controller);
/// The PageController that this DotsIndicator is representing.
final PageController controller;
/// The number of items managed by the PageController
final int itemCount;
/// Called when a dot is tapped
final ValueChanged<int> onPageSelected;
/// The color of the dots.
///
/// Defaults to `Colors.white`.
final Color color;
// The base size of the dots
final double dotSize;
// The increase in the size of the selected dot
final double maxZoom;
// The distance between the center of each dot
final double dotSpacing;
Widget _buildDot(int index) {
double selectedness = Curves.easeOut.transform(
max(
0.0,
1.0 - ((controller.page ?? controller.initialPage) - index).abs(),
),
);
double zoom = 1.0 + (maxZoom - 1.0) * selectedness;
return new Container(
width: dotSpacing,
child: new Center(
child: new Material(
color: color,
type: MaterialType.circle,
child: new Container(
width: dotSize * zoom,
height: dotSize * zoom,
child: new InkWell(
onTap: () => onPageSelected(index),
),
),
),
),
);
}
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
return Container(
height: (dotSize * maxZoom) + 1,
constraints: BoxConstraints(maxWidth: constraints.maxWidth / 2),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: new List<Widget>.generate(itemCount, _buildDot),
),
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment