Skip to content

Instantly share code, notes, and snippets.

@alamsyahh15
Created March 25, 2021 17:11
Show Gist options
  • Save alamsyahh15/b728a85ae7f3b9d79a7648af676cacf1 to your computer and use it in GitHub Desktop.
Save alamsyahh15/b728a85ae7f3b9d79a7648af676cacf1 to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
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: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List dataList = [];
List currentDataList = [];
int page = 1;
int pageCount = 5;
int startAt = 0;
int endAt;
int totalPages = 0;
@override
void initState() {
for (var i = 1; i <= 100; i++) dataList.add("Test - $i");
endAt = startAt + pageCount;
totalPages = (dataList.length / pageCount).floor();
if (dataList.length / pageCount > totalPages) {
totalPages = totalPages + 1;
}
currentDataList = dataList.getRange(startAt, endAt).toList();
super.initState();
}
void loadPreviousPage() {
if (page > 1) {
setState(() {
startAt = startAt - pageCount;
endAt = page == totalPages
? endAt - currentDataList.length
: endAt - pageCount;
currentDataList = dataList.getRange(startAt, endAt).toList();
page = page - 1;
});
}
}
void loadNextPage() {
if (page < totalPages) {
setState(() {
startAt = startAt + pageCount;
endAt = dataList.length > endAt + pageCount
? endAt + pageCount
: dataList.length;
currentDataList = dataList.getRange(startAt, endAt).toList();
page = page + 1;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Pagination'),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: currentDataList.length,
itemBuilder: (context, index) {
return Card(
elevation: 5,
child: Text(
currentDataList[index],
),
);
},
),
),
ElevatedButton(
child: Text("Change"),
onPressed: (){
dataList.replaceRange(startAt,endAt,["hallo1","hallo2","hallo3","hallo4","hallo5"]);
currentDataList.replaceRange((currentDataList.length - currentDataList.length),currentDataList.length,["hallo1","hallo2","hallo3","hallo4","hallo5"]);
setState((){});
},
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
IconButton(
onPressed: page > 1 ? loadPreviousPage : null,
icon: Icon(
Icons.arrow_back_ios,
size: 35,
),
),
Text("$page / $totalPages"),
IconButton(
onPressed: page < totalPages ? loadNextPage : null,
icon: Icon(
Icons.arrow_forward_ios,
size: 35,
),
),
],
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment