Skip to content

Instantly share code, notes, and snippets.

@Abdulsametileri
Created February 11, 2021 10:14
Show Gist options
  • Save Abdulsametileri/ca43b8ab069b5bb508a989e627d24981 to your computer and use it in GitHub Desktop.
Save Abdulsametileri/ca43b8ab069b5bb508a989e627d24981 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:infinite_scroll/viewmodel/cards_view_model.dart';
import 'card_item_view.dart';
class CardsView extends StatefulWidget {
@override
_CardsViewState createState() => _CardsViewState();
}
class _CardsViewState extends State<CardsView> {
CardsViewModel _vm;
@override
void initState() {
_vm = CardsViewModel();
_vm.init();
_vm.fetchCards();
super.initState();
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Cards'),
),
body: Observer(builder: (_) {
if (_vm.cards.isEmpty) {
return _buildLoading;
}
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemCount: _vm.cards.length + 1,
itemBuilder: (context, index) {
if (index == _vm.cards.length) {
_vm.fetchCards();
return _buildLoading;
}
return CardItemView(card: _vm.cards[index]);
},
);
}),
);
}
Center get _buildLoading {
return Center(
child: CircularProgressIndicator(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment