【问题标题】:Flutter Swiper, Be on same page after poping from another screenFlutter Swiper,从另一个屏幕弹出后在同一页面上
【发布时间】:2020-03-22 21:31:36
【问题描述】:

我正在使用Flutter Swiper 插件来创建一个刷卡器。在 swiper 的每一页上,我都有使用 Navigator.of(context).pushNamed('/routeToSecondPage') 将我带到另一个屏幕的按钮。

我面临的问题是,当我从该页面返回时,Swiper 会重新加载并返回其初始页面(即 0)。我希望它位于按下按钮的同一页面上。

我尝试使用 PageView,它在 keepPage = true 上可以正常工作,但由于 DotIndicator、无限循环等一些功能,我更倾向于使用 Flutter_swiper。

这是我的刷卡代码:

class SwiperHomePage extends StatefulWidget {


@override
  _SwiperHomePageState createState() => _SwiperHomePageState();
}

class _SwiperHomePageState extends State<_SwiperHomePage> {
  static int _swiperIndex = 0;
  final SwiperController _controller = SwiperController();

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child:Swiper(
              index: _swiperIndex,
              loop: true,
              scrollDirection: Axis.horizontal,
              indicatorLayout: PageIndicatorLayout.SCALE,
              itemBuilder: (ctx, i) {
                return Container(
                  margin: EdgeInsets.only(top: 24),
                  child: Container(
                          margin: EdgeInsets.all(4),
                          child: EachPage(),
                        ),
                );
              },
              itemCount: length,
              controller: _controller,
              pagination: SwiperPagination(
                alignment: Alignment.topCenter,
                builder: DotSwiperPaginationBuilder(
                    color: Colors.lightBlueAccent, activeColor: Colors.blue),
              ),
              duration: 1000,
              plugins: [],
            ),
    );
  }

  changeSwiperIndex(int index) {
    _controller.move(index, animation: true);
  }
}

【问题讨论】:

    标签: android flutter swiper


    【解决方案1】:

    想出了如何做到这一点:

    我保留了 _swiperIndex,但在页面更改时没有更改它。 在 swiper 中添加以下内容就可以了。

              onIndexChanged: (index) {
                _swiperIndex = index;
              },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 2018-03-04
      • 1970-01-01
      • 2019-10-27
      • 2019-10-10
      • 1970-01-01
      相关资源
      最近更新 更多