Fix: Lazy loader on web and its indicator
This commit is contained in:
@@ -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(),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user