Flutter分页加载插件flutter_pagination的使用
Flutter分页加载插件flutter_pagination的使用
使用分页在Flutter中
示例1:不使用分组
import 'package:flutter/material.dart';
import 'package:flutter_pagination/flutter_pagination.dart';
import 'package:flutter_pagination/widgets/button_styles.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int currentPage = 1;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Pagination Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Current Page: $currentPage'),
SizedBox(height: 20),
Pagination(
paginateButtonStyles: PaginateButtonStyles(),
prevButtonStyles: PaginateSkipButton(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20))),
nextButtonStyles: PaginateSkipButton(
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
bottomRight: Radius.circular(20))),
onPageChange: (number) {
setState(() {
currentPage = number;
});
},
useGroup: false,
totalPage: 30,
show: 2,
currentPage: currentPage,
),
],
),
),
);
}
}
示例2:使用分组
import 'package:flutter/material.dart';
import 'package:flutter_pagination/flutter_pagination.dart';
import 'package:flutter_pagination/widgets/button_styles.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int currentPage = 1;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Pagination Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Current Page: $currentPage'),
SizedBox(height: 20),
Pagination(
width: MediaQuery.of(context).size.width * .6, // 这个属性是可选的
paginateButtonStyles: PaginateButtonStyles(),
prevButtonStyles: PaginateSkipButton(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20))),
nextButtonStyles: PaginateSkipButton(
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
bottomRight: Radius.circular(20))),
onPageChange: (number) {
setState(() {
currentPage = number;
});
},
useGroup: true,
totalPage: 30,
show: 4,
currentPage: currentPage,
),
],
),
),
);
}
}
注意事项
- 如果显示值大于总页数,将会抛出错误。
- 高度值默认为50,并且仅在分组分页中生效。宽度也是如此。
样式设置
import 'package:flutter/material.dart';
import 'package:flutter_pagination/flutter_pagination.dart';
import 'package:flutter_pagination/widgets/button_styles.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int currentPage = 1;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Pagination Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Current Page: $currentPage'),
SizedBox(height: 20),
Pagination(
paginateButtonStyles: PaginateButtonStyles(
backgroundColor: Colors.pink,
activeBackgroundColor: Colors.black,
activeTextStyle: const TextStyle(color: Colors.red)),
prevButtonStyles: PaginateSkipButton(
buttonBackgroundColor: Colors.orange,
icon: const Icon(
Icons.arrow_back_ios,
size: 30,
),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
bottomLeft: Radius.circular(20))),
nextButtonStyles: PaginateSkipButton(
buttonBackgroundColor: Colors.purple,
icon: const Icon(
Icons.arrow_right_alt,
color: Colors.white,
),
borderRadius: const BorderRadius.only(
topRight: Radius.circular(20),
bottomRight: Radius.circular(20))),
onPageChange: (number) {
setState(() {
currentPage = number;
});
},
useGroup: true,
totalPage: 30,
show: 4,
currentPage: currentPage,
),
],
),
),
);
}
}
必填字段
字段名称 | 类型 |
---|---|
totalPage | integer |
currentPage | integer |
onPageChange | (number) => void |
nextButtonStyles | PaginateSkipButton |
prevButtonStyles | PaginateSkipButton |
paginateButtonStyles | PaginateButtonStyles |
希望这些示例和说明对你有所帮助!如果你有任何问题或需要进一步的帮助,请随时提问。
更多关于Flutter分页加载插件flutter_pagination的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复