如何计算点指标的横幅/页面数量?我使用Smooth_page_indicator软件包在pub.dev上可用

发布于 2025-01-29 06:24:48 字数 2808 浏览 3 评论 0原文

我必须返回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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-02-05 06:24:48

为pageView创建一个变量

List<Widget> _pages = [some Container]
int _currentIndex = 0;

,并在OnPageChanged()上对其进行更新,

onPageChanged: ((page) {
  setState(() {
    _currentIndex = page.toInt();
   });
}),

请计数使用的页面中的页面数:_pages.length

但我认为,二手包

Create a variable for children of PageView

List<Widget> _pages = [some Container]
int _currentIndex = 0;

And update it on onPageChanged()

onPageChanged: ((page) {
  setState(() {
    _currentIndex = page.toInt();
   });
}),

Count the number of pages in the PageView used: _pages.length

But I think, used package smooth_page_indicator is best way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文