第一次加载数据存在问题,没有更新记录和优化MVC结构中的代码,并且没有任何使用包。
完整代码在此链接https://github.com/RajTechnologiesPvtLtd/flutter-moonlight/blob/master/lib/views/example_mvc/LoadMoreViewMVC.dart
config
class ApiEndpoint {
static const loadMoreUrl = 'https://jsonplaceholder.typicode.com/posts';
}
字符串
LoadMoreModelMVC(模型)
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';
import '../config/config.dart';
class LoadMoreModelMVC {
int _page = 0;
final int _limit = 20;
bool _hasNextPage = true;
bool _isFirstLoadRunning = false;
bool _isLoadMoreRunning = false;
List _records = [];
final _baseUrl = ApiEndpoint.loadMoreUrl;
late ScrollController _controller;
// Getters
List get records => _records;
bool get hasNextPage => _hasNextPage;
Future<void> firstLoad() async {
_isFirstLoadRunning = true;
try {
final res =
await http.get(Uri.parse("$_baseUrl?_page=$_page&_limit=$_limit"));
_records = json.decode(res.body);
} catch (err) {
print('Something went wrong');
}
_isFirstLoadRunning = false;
}
Future<void> loadMore() async {
if (_hasNextPage == true &&
_isFirstLoadRunning == false &&
_isLoadMoreRunning == false &&
_controller.position.extentAfter < 300) {
_isLoadMoreRunning = true;
_page += 1;
try {
final res =
await http.get(Uri.parse("$_baseUrl?_page=$_page&_limit=$_limit"));
final List fetchedRecords = json.decode(res.body);
if (fetchedRecords.isNotEmpty) {
_records.addAll(fetchedRecords);
} else {
_hasNextPage = false;
}
} catch (err) {
print('Something went wrong!');
}
_isLoadMoreRunning = false;
}
}
void initController() {
_controller = ScrollController();
}
void disposeController() {
_controller.dispose();
}
ScrollController getController() {
return _controller;
}
// List getRecords() {
// return _records;
// }
// bool getIsFirstLoadRunning() {
// return _isFirstLoadRunning;
// }
// bool getIsLoadMoreRunning() {
// return _isLoadMoreRunning;
// }
// bool getHasNextPage() {
// return _hasNextPage;
// }
Future<void> refresh() async {
_page = 0;
_hasNextPage = true;
_records = [];
await firstLoad();
}
}
型
LoadMoreControllerMVC(控制器)
import '../models/LoadMoreModelMVC.dart';
class LoadMoreControllerMVC {
final LoadMoreModelMVC model = LoadMoreModelMVC();
Future<void> fetchFirstRecords() async {
await model.firstLoad();
}
Future<void> fetchMoreRecords() async {
await model.loadMore();
}
}
型
LoadMoreViewMVC(视图)
import 'package:flutter/material.dart';
import '../../controllers/controllers.dart';
import '../../widgets/widgets.dart';
class LoadMoreViewMVC extends StatefulWidget {
const LoadMoreViewMVC({Key? key}) : super(key: key);
@override
State<LoadMoreViewMVC> createState() => _LoadMoreViewMVCState();
}
class _LoadMoreViewMVCState extends State<LoadMoreViewMVC> {
final LoadMoreControllerMVC controller = LoadMoreControllerMVC();
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
controller.fetchFirstRecords();
_scrollController.addListener(_loadMore);
}
@override
void dispose() {
_scrollController.removeListener(_loadMore);
super.dispose();
}
void _loadMore() {
final double scrollExtent = _scrollController.position.maxScrollExtent;
final double currentPosition = _scrollController.position.pixels;
final double delta = scrollExtent - currentPosition;
if (delta < 200 && controller.model.hasNextPage) {
controller.fetchMoreRecords();
}
}
Widget _buildList() {
if (controller.model.records.isEmpty) {
return const Center(child: CircularProgressIndicator());
} else {
return ListView.builder(
controller: _scrollController,
itemCount: controller.model.records.length +
(controller.model.hasNextPage ? 1 : 0),
itemBuilder: (_, index) {
if (index == controller.model.records.length) {
return const Center(child: CircularProgressIndicator());
} else {
final record = controller.model.records[index];
return Card(
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
child: ListTile(
title: Text(record['title']),
),
);
}
},
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: const MoonLightDrawer("Load More MVC"),
appBar: const Navbar(title: "Load More MVC"),
body: _buildList(),
);
}
}
型
1条答案
按热度按时间5lwkijsr1#
请回答我的风格和Flutter的MVC代码,并没有使用任何Flutter包。