如何计算点指标的横幅/页面数量?我使用Smooth_page_indicator软件包在pub.dev上可用
我必须返回pageView中的页数,以便在dotsanimatedwidget-> AnimatedSmoothIndicator中的“计数”值自动更新,如果横幅数量增加或减少。有什么方法可以计算页面浏览量中的页数或计算儿童数量?我应该采取不同的方式吗? 使用的软件包的链接: https://pub.dev/packages/packages/smooth_page_page_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),
),
],
),
),
);
}
}
I have to return the number of pages in the PageView so that the value of 'count' in DotsAnimatedWidget->AnimatedSmoothIndicator automatically updates if the number of banners are increased or decreased. Is there any way to count the number of pages in the PageView or count the number of children? Should I approach a different way?
Link for the package used:
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),
),
],
),
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为pageView创建一个变量
,并在OnPageChanged()上对其进行更新,
请计数使用的页面中的页面数:
_pages.length
,但我认为,二手包
Create a variable for children of PageView
And update it on onPageChanged()
Count the number of pages in the PageView used:
_pages.length
But I think, used package smooth_page_indicator is best way