【发布时间】: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);
}
}
【问题讨论】: