-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pagination first page to work with static values
- Loading branch information
1 parent
0b839a5
commit 37b0350
Showing
5 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
2 changes: 2 additions & 0 deletions
2
pokedex/lib/features/pokemon_list/data/pokemon_list_repository.dart
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
32 changes: 30 additions & 2 deletions
32
pokedex/lib/features/pokemon_list/presentation/pokemon_list_page.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,40 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:pokedex/features/pokemon_list/presentation/bloc/pokemon_list_bloc.dart'; | ||
import 'package:pokedex/features/pokemon_list/presentation/widgets/pokemon_success.dart'; | ||
|
||
class PokemonListPage extends StatelessWidget { | ||
const PokemonListPage({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Scaffold( | ||
body: Text('Pokemon List'), | ||
return BlocProvider( | ||
create: (_) => GetIt.instance.get<PokemonListBloc>() | ||
..add( | ||
const PokemonListEventRequest(), | ||
), | ||
child: Scaffold( | ||
body: SafeArea( | ||
child: BlocBuilder<PokemonListBloc, PokemonListState>( | ||
builder: (context, state) { | ||
final pageState = state.pageStatus; | ||
if (state.firstPage) { | ||
if (pageState == PageStatus.loading) { | ||
return const Center( | ||
child: CircularProgressIndicator(), | ||
); | ||
} else if (pageState == PageStatus.failure) { | ||
return const Text('Error'); | ||
} else if (pageState == PageStatus.initial) { | ||
return const SizedBox.shrink(); | ||
} | ||
} | ||
return const PokemonSuccess(); | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
pokedex/lib/features/pokemon_list/presentation/widgets/pokemon_success.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:pokedex/design/base/spacing.dart'; | ||
import 'package:pokedex/features/pokemon_list/presentation/bloc/pokemon_list_bloc.dart'; | ||
import 'package:pokedex/features/pokemon_list/presentation/widgets/pokemon_card.dart'; | ||
|
||
class PokemonSuccess extends StatefulWidget { | ||
const PokemonSuccess({super.key}); | ||
|
||
@override | ||
State<PokemonSuccess> createState() => _PokemonSuccessState(); | ||
} | ||
|
||
class _PokemonSuccessState extends State<PokemonSuccess> { | ||
final _scrollController = ScrollController(); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_scrollController.addListener(_onScrollListener); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_scrollController | ||
..removeListener(_onScrollListener) | ||
..dispose(); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final textTheme = Theme.of(context).textTheme; | ||
final state = context.watch<PokemonListBloc>().state; | ||
final pageStatus = state.pageStatus; | ||
|
||
return CustomScrollView( | ||
controller: _scrollController, | ||
slivers: [ | ||
const SliverAppBar( | ||
snap: true, | ||
floating: true, | ||
pinned: true, | ||
scrolledUnderElevation: 0, | ||
expandedHeight: 120.0, | ||
flexibleSpace: FlexibleSpaceBar( | ||
centerTitle: false, | ||
titlePadding: EdgeInsets.symmetric(horizontal: Spacing.kL), | ||
title: Text('Hello World'), | ||
), | ||
), | ||
SliverPadding( | ||
padding: const EdgeInsets.symmetric(horizontal: Spacing.kL), | ||
sliver: SliverList.separated( | ||
separatorBuilder: (context, index) => const SizedBox( | ||
height: Spacing.kM, | ||
), | ||
itemBuilder: (context, index) { | ||
final pokemon = state.result[index]; | ||
return PokemonCard(); | ||
}, | ||
itemCount: state.result.length, | ||
), | ||
), | ||
SliverList( | ||
delegate: SliverChildBuilderDelegate( | ||
(_, __) { | ||
final Widget tailWidget; | ||
if (!state.firstPage && pageStatus == PageStatus.loading) { | ||
tailWidget = const Padding( | ||
padding: EdgeInsets.all(Spacing.kM), | ||
child: Center( | ||
child: CircularProgressIndicator(), | ||
)); | ||
} else if (!state.firstPage && pageStatus == PageStatus.failure) { | ||
tailWidget = Padding( | ||
padding: const EdgeInsets.symmetric( | ||
vertical: Spacing.kM, horizontal: Spacing.kL), | ||
child: Column( | ||
children: [ | ||
Text('Error title'), | ||
], | ||
), | ||
); | ||
} else { | ||
tailWidget = const SizedBox.shrink(); | ||
} | ||
return Padding( | ||
padding: const EdgeInsets.symmetric(vertical: Spacing.kXL), | ||
child: tailWidget, | ||
); | ||
}, | ||
childCount: 1, | ||
), | ||
) | ||
], | ||
); | ||
} | ||
|
||
void _onScrollListener() { | ||
if (_isBottomReached) { | ||
// request other | ||
} | ||
} | ||
|
||
bool get _isBottomReached { | ||
if (!_scrollController.hasClients) { | ||
return false; | ||
} | ||
final maxScroll = _scrollController.position.maxScrollExtent; | ||
final currentScroll = _scrollController.offset; | ||
return currentScroll >= (maxScroll * 0.9); | ||
} | ||
} |