点击时的 Flutter Swiper 抛出断言失败
Flutter Swiper on tap throws Failed assertion
我已经实现了一个Swier widget,可以在卡片中显示图书信息,下一步就是卡片被贴上去的时候推送一个新的页面,这里是代码:
class CardSwiper extends StatelessWidget {
final List<Book> books;
CardSwiper(this.books);
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.blueGrey,
body: new Swiper(
//containerHeight: 25.0,
itemBuilder: (BuildContext context, int index) {
return _getSwiperCardContent(index, context);
},
indicatorLayout: PageIndicatorLayout.COLOR,
autoplay: false,
itemCount: books.length,
pagination: null,
control: null,
viewportFraction: 0.6,
scale: 0.9,
));
}
_getSwiperCardContent(int index, BuildContext context){
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0),
),
elevation: 10,
color: Colors.white,
child: Column(
children: <Widget>[
Flexible(
child: Align(
alignment: Alignment.center,
child: InkWell(
onTap: _goToBookPage(context),
child: Image.network(
this.books[index].picture,
),
)
),
flex: 6,
),
Flexible(
child: Align(
alignment: Alignment.center,
child: Container(
child: Text(
this.books[index].title,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
flex: 1,
),
Flexible(
child: Align(
alignment: Alignment.center,
child: Text(
this.books[index].author,
style: TextStyle(
color: Colors.grey[500],
),
),
),
flex: 1,
)
],
),
);
}
_goToBookPage(BuildContext context){
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => BookPage("title", this.books)));
}
}
结果出现如下错误:
setState() 或 markNeedsBuild() 在构建期间调用。
'package:flutter/src/widgets/navigator.dart':断言失败:第 1762 行第 12 行:'!_debugLocked':不正确。
当 Swiper[=32= 时,我应该如何实现新的 _goToBookPage 方法] 项目被点击了?
与其那样做,不如用 GestureDetector Widget 包装整个 Widget,然后用匿名函数调用方法。
GestureDetector(
onTap: () {
// Your_method_call_here
},
child: Your_Widget(
....
)
);
我已经实现了一个Swier widget,可以在卡片中显示图书信息,下一步就是卡片被贴上去的时候推送一个新的页面,这里是代码:
class CardSwiper extends StatelessWidget {
final List<Book> books;
CardSwiper(this.books);
@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.blueGrey,
body: new Swiper(
//containerHeight: 25.0,
itemBuilder: (BuildContext context, int index) {
return _getSwiperCardContent(index, context);
},
indicatorLayout: PageIndicatorLayout.COLOR,
autoplay: false,
itemCount: books.length,
pagination: null,
control: null,
viewportFraction: 0.6,
scale: 0.9,
));
}
_getSwiperCardContent(int index, BuildContext context){
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7.0),
),
elevation: 10,
color: Colors.white,
child: Column(
children: <Widget>[
Flexible(
child: Align(
alignment: Alignment.center,
child: InkWell(
onTap: _goToBookPage(context),
child: Image.network(
this.books[index].picture,
),
)
),
flex: 6,
),
Flexible(
child: Align(
alignment: Alignment.center,
child: Container(
child: Text(
this.books[index].title,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
flex: 1,
),
Flexible(
child: Align(
alignment: Alignment.center,
child: Text(
this.books[index].author,
style: TextStyle(
color: Colors.grey[500],
),
),
),
flex: 1,
)
],
),
);
}
_goToBookPage(BuildContext context){
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => BookPage("title", this.books)));
}
}
结果出现如下错误:
setState() 或 markNeedsBuild() 在构建期间调用。
'package:flutter/src/widgets/navigator.dart':断言失败:第 1762 行第 12 行:'!_debugLocked':不正确。
当 Swiper[=32= 时,我应该如何实现新的 _goToBookPage 方法] 项目被点击了?
与其那样做,不如用 GestureDetector Widget 包装整个 Widget,然后用匿名函数调用方法。
GestureDetector(
onTap: () {
// Your_method_call_here
},
child: Your_Widget(
....
)
);