Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
library-app-flutter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ilham Maulana
library-app-flutter
Commits
fc2551fa
Commit
fc2551fa
authored
Aug 06, 2024
by
Ilham Maulana
💻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: paging book list
parent
50af315d
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
44 deletions
+106
-44
auth_provider.dart
lib/src/providers/auth_provider.dart
+3
-3
book_provider.dart
lib/src/providers/book_provider.dart
+17
-1
book_list.dart
lib/src/widgets/books/book_list.dart
+86
-40
No files found.
lib/src/providers/auth_provider.dart
View file @
fc2551fa
...
@@ -489,11 +489,11 @@ class AuthProvider with ChangeNotifier {
...
@@ -489,11 +489,11 @@ class AuthProvider with ChangeNotifier {
if
(
response
.
statusCode
==
200
)
{
if
(
response
.
statusCode
==
200
)
{
final
data
=
jsonDecode
(
response
.
body
);
final
data
=
jsonDecode
(
response
.
body
);
if
(
type
==
"upcoming"
)
{
if
(
type
==
"upcoming"
)
{
nearOutstandingLoans
=
data
;
nearOutstandingLoans
=
data
[
"data"
]
;
}
else
if
(
type
==
"overdue"
)
{
}
else
if
(
type
==
"overdue"
)
{
overduedLoans
=
data
;
overduedLoans
=
data
[
"data"
]
;
}
else
{
}
else
{
loans
=
data
;
loans
=
data
[
"data"
]
;
}
}
}
else
{
}
else
{
final
code
=
response
.
statusCode
;
final
code
=
response
.
statusCode
;
...
...
lib/src/providers/book_provider.dart
View file @
fc2551fa
...
@@ -12,6 +12,11 @@ class BookProvider with ChangeNotifier {
...
@@ -12,6 +12,11 @@ class BookProvider with ChangeNotifier {
String
?
searchKeyword
;
String
?
searchKeyword
;
String
?
filterByCategory
;
String
?
filterByCategory
;
bool
hasNextPage
=
false
;
bool
hasPrevPage
=
false
;
int
pageNumber
=
1
;
int
?
totalPages
;
bool
isLoading
=
false
;
bool
isLoading
=
false
;
void
setLoading
(
bool
value
)
{
void
setLoading
(
bool
value
)
{
...
@@ -26,6 +31,8 @@ class BookProvider with ChangeNotifier {
...
@@ -26,6 +31,8 @@ class BookProvider with ChangeNotifier {
url
+=
'?category=
$filterByCategory
'
;
url
+=
'?category=
$filterByCategory
'
;
}
else
if
(
searchKeyword
!=
null
)
{
}
else
if
(
searchKeyword
!=
null
)
{
url
+=
"?search=
$searchKeyword
"
;
url
+=
"?search=
$searchKeyword
"
;
}
else
if
(
pageNumber
!=
null
)
{
url
+=
"?page=
$pageNumber
"
;
}
}
final
response
=
await
http
.
get
(
final
response
=
await
http
.
get
(
...
@@ -35,7 +42,11 @@ class BookProvider with ChangeNotifier {
...
@@ -35,7 +42,11 @@ class BookProvider with ChangeNotifier {
if
(
response
.
statusCode
==
200
)
{
if
(
response
.
statusCode
==
200
)
{
final
data
=
jsonDecode
(
response
.
body
);
final
data
=
jsonDecode
(
response
.
body
);
books
=
data
;
books
=
data
[
"data"
];
hasNextPage
=
data
[
"has_next"
];
hasPrevPage
=
data
[
"has_prev"
];
pageNumber
=
data
[
"page_number"
];
totalPages
=
data
[
"total_pages"
];
}
else
{
}
else
{
final
code
=
response
.
statusCode
;
final
code
=
response
.
statusCode
;
debugPrint
(
"Error: Fetch books failed,
$code
"
);
debugPrint
(
"Error: Fetch books failed,
$code
"
);
...
@@ -58,6 +69,11 @@ class BookProvider with ChangeNotifier {
...
@@ -58,6 +69,11 @@ class BookProvider with ChangeNotifier {
notifyListeners
();
notifyListeners
();
}
}
void
setPage
(
int
value
)
{
pageNumber
=
value
;
notifyListeners
();
}
Future
<
void
>
getCategories
()
async
{
Future
<
void
>
getCategories
()
async
{
try
{
try
{
setLoading
(
true
);
setLoading
(
true
);
...
...
lib/src/widgets/books/book_list.dart
View file @
fc2551fa
...
@@ -21,11 +21,49 @@ class _BookList extends State<BookList> {
...
@@ -21,11 +21,49 @@ class _BookList extends State<BookList> {
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
getBooks
();
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
getBooks
();
}
}
@override
void
dispose
()
{
super
.
dispose
();
}
ScrollController
listScrollController
=
ScrollController
();
void
scrollToTop
()
{
if
(
listScrollController
.
hasClients
)
{
final
position
=
listScrollController
.
position
.
minScrollExtent
;
listScrollController
.
jumpTo
(
position
);
}
}
Future
<
void
>
nextPage
()
async
{
if
(
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
hasNextPage
)
{
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
setPage
(
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
pageNumber
+
1
,
);
}
else
{
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
setPage
(
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
totalPages
!);
}
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
getBooks
();
scrollToTop
();
}
Future
<
void
>
prevPage
()
async
{
if
(
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
hasPrevPage
)
{
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
setPage
(
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
pageNumber
-
1
,
);
}
else
{
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
setPage
(
1
);
}
Provider
.
of
<
BookProvider
>(
context
,
listen:
false
).
getBooks
();
scrollToTop
();
}
@override
@override
Widget
build
(
BuildContext
context
)
{
Widget
build
(
BuildContext
context
)
{
return
Consumer
<
BookProvider
>(
return
Consumer
<
BookProvider
>(
builder:
(
context
,
bookProvider
,
child
)
{
builder:
(
context
,
bookProvider
,
child
)
{
if
(!
bookProvider
.
isLoading
)
{
final
Iterable
<
Book
>
books
=
bookProvider
.
books
!.
map
((
book
)
{
final
Iterable
<
Book
>
books
=
bookProvider
.
books
!.
map
((
book
)
{
if
(
book
[
"category"
]
!=
null
)
{
if
(
book
[
"category"
]
!=
null
)
{
final
Category
category
=
Category
.
fromJson
(
final
Category
category
=
Category
.
fromJson
(
...
@@ -52,29 +90,37 @@ class _BookList extends State<BookList> {
...
@@ -52,29 +90,37 @@ class _BookList extends State<BookList> {
});
});
return
NestedScrollView
(
return
NestedScrollView
(
headerSliverBuilder:
headerSliverBuilder:
(
BuildContext
context
,
bool
innerBoxIsScrolled
)
{
(
BuildContext
context
,
bool
innerBoxIsScrolled
)
{
return
[
const
TopAppBar
(
title:
"Books"
)];
return
[
const
TopAppBar
(
title:
"Books"
)];
},
},
body:
ListView
(
body:
ListView
.
builder
(
children:
List
.
generate
(
books
.
length
,
(
index
)
{
controller:
listScrollController
,
return
BookItem
(
itemCount:
books
.
length
+
1
,
books
.
elementAt
(
index
),
itemBuilder:
(
context
,
index
)
{
);
if
(
index
<
books
.
length
)
{
}),
return
BookItem
(
books
.
elementAt
(
index
));
}
else
{
return
Container
(
padding:
const
EdgeInsets
.
all
(
10
),
child:
Row
(
mainAxisAlignment:
MainAxisAlignment
.
spaceBetween
,
children:
[
ElevatedButton
(
onPressed:
prevPage
,
child:
const
Text
(
'Prev'
),
),
Text
(
bookProvider
.
pageNumber
.
toString
()),
ElevatedButton
(
onPressed:
nextPage
,
child:
const
Text
(
'Next'
),
),
],
),
),
);
);
}
else
{
}
return
NestedScrollView
(
headerSliverBuilder:
(
BuildContext
context
,
bool
innerBoxIsScrolled
)
{
return
[
const
TopAppBar
(
title:
"Books"
)];
},
},
body:
const
Center
(
child:
CircularProgressIndicator
(),
),
),
);
);
}
},
},
);
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment