Fix: Lazy loader on web and its indicator

This commit is contained in:
2024-05-31 14:00:34 +03:00
parent 0438a6feec
commit 3b593ad733
3 changed files with 46 additions and 6 deletions

View File

@@ -36,7 +36,7 @@ class BasketItemCard extends StatelessWidget {
),
child: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacement(
Navigator.of(context).push(
CustomPageRoute(builder: (context) => DetailPage(id: id)));
},
child: Card(

View File

@@ -69,6 +69,8 @@ class _MainPageState extends State<MainPage> {
List<GymItem> filteredData = [];
List<GymItem> items = [];
int cartLength = 0;
int itemViewCount = 0;
bool isLoading = false;
@override
void initState() {
@@ -81,6 +83,7 @@ class _MainPageState extends State<MainPage> {
getItems(context).then((value) => setState(() {
filteredData = value;
items = value;
itemViewCount = min(5, value.length);
}));
}
@@ -92,8 +95,14 @@ class _MainPageState extends State<MainPage> {
}
void _onLoad() async {
await Future.delayed(const Duration(milliseconds: 1000));
debugPrint('aye');
setState(() {
isLoading = true;
});
await Future.delayed(const Duration(seconds: 1));
setState(() {
itemViewCount = min(filteredData.length, itemViewCount + 5);
isLoading = false;
});
}
void _onSearch() {
@@ -101,6 +110,7 @@ class _MainPageState extends State<MainPage> {
filteredData = items
.where((element) => (element.title).contains(searchText))
.toList();
itemViewCount = filteredData.length;
});
}
@@ -190,13 +200,16 @@ class _MainPageState extends State<MainPage> {
Expanded(
child: LazyLoadScrollView(
onEndOfPage: _onLoad,
child: Stack(
isLoading: isLoading,
child: ListView(
children: [
items.isEmpty
? const Center(child: CircularProgressIndicator())
: filteredData.isEmpty
? const Center(child: Text('Ничего не найдено'))
: GridView.builder(
physics: const AlwaysScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: min(
@@ -204,7 +217,7 @@ class _MainPageState extends State<MainPage> {
.toInt(),
8),
),
itemCount: filteredData.length,
itemCount: itemViewCount,
itemBuilder: (context, index) {
final product = filteredData[index];
return ProductCard(
@@ -225,6 +238,34 @@ class _MainPageState extends State<MainPage> {
);
},
),
itemViewCount > 0
? Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Center(
child: itemViewCount < filteredData.length
? !isLoading
? ElevatedButton(
onPressed: _onLoad,
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context).primaryColor,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50)),
),
foregroundColor: Colors.white,
),
child: const Text('Загрузить ещё'),
)
: const CircularProgressIndicator()
: const Text(
'Конец списка',
style: TextStyle(
fontSize: 10, color: Color(0x88000000)),
),
),
)
: const SizedBox.shrink(),
],
),
),

View File

@@ -32,7 +32,6 @@ Future<List<GymItem>> getItems(BuildContext context) async {
return [];
}
//FIXME: Сделать, чтоб работало
Future<List<GymItem>> getItemsByIds(
BuildContext context, List<String> ids) async {
final token = context.read<GymLinkProvider>().token;