Skip to content

Instantly share code, notes, and snippets.

@xpwmaosldk
Last active July 14, 2021 06:35
Show Gist options
  • Save xpwmaosldk/ed898317585b84a4b6dc1700550f72a4 to your computer and use it in GitHub Desktop.
Save xpwmaosldk/ed898317585b84a4b6dc1700550f72a4 to your computer and use it in GitHub Desktop.
https://github.com/xpwmaosldk/number_pagination 에서 관리합니다. 아래 로직엔 문제가 있습니다.(공부 할 겸 풀어보시는 것도 좋아요)
//deprecated
import 'package:flutter/material.dart';
class Pagination extends StatefulWidget {
final Function(int) listner;
final int totalPage;
final int currentPage;
final int size;
Pagination({this.listner, this.totalPage, this.currentPage, this.size = 10});
@override
_PaginationState createState() => _PaginationState();
}
class _PaginationState extends State<Pagination> {
int currentPage = 0;
int rangeStart = 0;
int rangeEnd = 0;
@override
void initState() {
rangeStart = (currentPage ~/ widget.size) * widget.size;
rangeEnd = rangeStart + widget.size;
super.initState();
}
void changePage(int page) {
if (page < 0) page = 0;
if (page > widget.totalPage - 1) page = widget.totalPage - 1;
setState(() {
currentPage = page;
rangeStart = (currentPage ~/ widget.size) * widget.size;
rangeEnd = rangeStart + widget.size;
widget.listner(currentPage);
});
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => changePage(0),
child: Icon(Icons.first_page),
),
SizedBox(
width: 4,
),
ElevatedButton(
onPressed: () => changePage(--currentPage),
child: Icon(Icons.keyboard_arrow_left),
),
SizedBox(
width: 10,
),
...List.generate(
rangeEnd < widget.totalPage ? widget.size : widget.totalPage % widget.size,
(index) => Flexible(
child: InkWell(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () => changePage(index),
child: Container(
margin: const EdgeInsets.all(4),
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
decoration: BoxDecoration(
color: currentPage % widget.size == index ? Theme.of(context).primaryColor : Colors.white,
borderRadius: BorderRadius.all(Radius.circular(4)),
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 1.0), //(x,y)
blurRadius: 6.0,
),
],
),
child: Text(
'${index + 1 + rangeStart}',
style: TextStyle(
color: currentPage % widget.size == index ? Colors.white : Colors.black,
),
),
),
),
),
),
SizedBox(
width: 10,
),
ElevatedButton(
onPressed: () => changePage(++currentPage),
child: Icon(Icons.keyboard_arrow_right),
),
SizedBox(
width: 4,
),
ElevatedButton(
onPressed: () => changePage(widget.totalPage),
child: Icon(Icons.last_page),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment