如何计算点指标的banners/pages个数?我正在使用 pub.dev 上可用的 smooth_page_indicator 软件包
How to count the number of banners/pages for dot indicators? I'm using smooth_page_indicator package available on pub.dev
我必须 return PageView 中的页数,以便 DotsAnimatedWidget->AnimatedSmoothIndicator 中 'count' 的值在横幅数量增加或减少时自动更新。有没有办法统计PageView的页数或者统计children的个数?我应该换一种方式吗?
Link 使用的包:
https://pub.dev/packages/smooth_page_indicator
class _BannerWidgetState extends State<BannerWidget> {
int _pages = 0;
final _controller = PageController();
@override
Widget build(BuildContext context) {
return Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Container(
height: 140,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: PageView(
controller: _controller,
onPageChanged: ((val) {
setState(() {
_pages = val.toInt();
});
}),
children: const [
Center(
child: Text(
'Banner 1',
style: TextStyle(fontSize: 20),
),
),
Center(
child: Text(
'Banner 2',
style: TextStyle(fontSize: 20),
),
),
Center(
child: Text(
'Banner 3',
style: TextStyle(fontSize: 20),
),
),
],
),
),
),
),
DotsIndicatorWidget(pages: _pages),
],
);
}
}
class DotsIndicatorWidget extends StatelessWidget {
const DotsIndicatorWidget({
Key? key,
required int pages,
}) : _pages = pages,
super(key: key);
final int _pages;
@override
Widget build(BuildContext context) {
return Positioned.fill(
bottom: 18,
child: Align(
alignment: Alignment.bottomCenter,
child: Row(
//mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedSmoothIndicator(
activeIndex: _pages,
count: 3,
effect: const WormEffect(
spacing: 4.0,
activeDotColor: Colors.green,
dotColor: Colors.grey,
dotHeight: 8.0,
dotWidth: 8.0,
radius: 3,
type: WormType.normal),
),
],
),
),
);
}
}
为 PageView 的子项创建一个变量
List<Widget> _pages = [some Container]
int _currentIndex = 0;
并在 onPageChanged() 上更新它
onPageChanged: ((page) {
setState(() {
_currentIndex = page.toInt();
});
}),
计算使用的 PageView 中的页面数:_pages.length
但我认为,使用包 smooth_page_indicator 是最好的方法
我必须 return PageView 中的页数,以便 DotsAnimatedWidget->AnimatedSmoothIndicator 中 'count' 的值在横幅数量增加或减少时自动更新。有没有办法统计PageView的页数或者统计children的个数?我应该换一种方式吗? Link 使用的包: https://pub.dev/packages/smooth_page_indicator
class _BannerWidgetState extends State<BannerWidget> {
int _pages = 0;
final _controller = PageController();
@override
Widget build(BuildContext context) {
return Stack(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 10),
child: ClipRRect(
borderRadius: BorderRadius.circular(5),
child: Container(
height: 140,
width: MediaQuery.of(context).size.width,
color: Colors.white,
child: PageView(
controller: _controller,
onPageChanged: ((val) {
setState(() {
_pages = val.toInt();
});
}),
children: const [
Center(
child: Text(
'Banner 1',
style: TextStyle(fontSize: 20),
),
),
Center(
child: Text(
'Banner 2',
style: TextStyle(fontSize: 20),
),
),
Center(
child: Text(
'Banner 3',
style: TextStyle(fontSize: 20),
),
),
],
),
),
),
),
DotsIndicatorWidget(pages: _pages),
],
);
}
}
class DotsIndicatorWidget extends StatelessWidget {
const DotsIndicatorWidget({
Key? key,
required int pages,
}) : _pages = pages,
super(key: key);
final int _pages;
@override
Widget build(BuildContext context) {
return Positioned.fill(
bottom: 18,
child: Align(
alignment: Alignment.bottomCenter,
child: Row(
//mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedSmoothIndicator(
activeIndex: _pages,
count: 3,
effect: const WormEffect(
spacing: 4.0,
activeDotColor: Colors.green,
dotColor: Colors.grey,
dotHeight: 8.0,
dotWidth: 8.0,
radius: 3,
type: WormType.normal),
),
],
),
),
);
}
}
为 PageView 的子项创建一个变量
List<Widget> _pages = [some Container]
int _currentIndex = 0;
并在 onPageChanged() 上更新它
onPageChanged: ((page) {
setState(() {
_currentIndex = page.toInt();
});
}),
计算使用的 PageView 中的页面数:_pages.length
但我认为,使用包 smooth_page_indicator 是最好的方法