我正在使用Flutter 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);
}
}
onIndexChanged: (index) {
_swiperIndex = index;
},