【发布时间】:2021-06-24 07:44:14
【问题描述】:
我有一个看起来像字典列表的对象 fixedTest,我正在从中获取数据以创建一个滚动视图对象。原始的 ListWheelScrollView 不捕获点击,所以我使用来自包 clickable_list_wheel_view 的自定义小部件。当我尝试启动测试代码时:
body: ClickableListWheelScrollView(
scrollController: _scrollController,
itemHeight: 100,
itemCount: 100,
onItemTapCallback: (index) {
print("onItemTapCallback index: $index");
},`
错误:
[错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常: 'package:flutter/src/widgets/scroll_controller.dart': 失败 断言:第 108 行 pos 12:'_positions.isNotEmpty':ScrollController 未附加到任何滚动视图。
代码:
launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url, forceWebView: true);
} else {
throw 'Could not launch $url';
}
}
@override
Widget build (BuildContext context) {
test = (ModalRoute.of(context).settings.arguments);
var fixedTest = test['data'].news4today;
final _scrollController = ScrollController();
checkUrls() {
for (int i = 0; i < fixedTest.length; i++) {
if (fixedTest[i]['urlToImage'] == null ||
!isURL(fixedTest[i]['urlToImage'])) {
fixedTest[i]['urlToImage'] =
'https://i.pinimg.com/originals/8a/eb/d8/8aebd875fbddd22bf3971c3a7159bdc7.png';
}
if (fixedTest[i]['url'] == null || !isURL(fixedTest[i]['url'])) {
fixedTest[i]['url'] = 'https://google.com';
}
}
}
checkUrls();
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Center(child: Text('News')),
backgroundColor: Colors.deepPurpleAccent,
),
body: ClickableListWheelScrollView(
scrollController: _scrollController,
itemHeight: 100,
itemCount: 100,
onItemTapCallback: (index) {
print("onItemTapCallback index: $index");
},
child: ListWheelScrollView.useDelegate(
itemExtent: 400,
diameterRatio: 9,
squeeze: 1.2,
physics: BouncingScrollPhysics(),
onSelectedItemChanged: (index) {
// debugPrint(index.toString());
},
childDelegate: ListWheelChildBuilderDelegate(
childCount: 100,
builder: (context, index) => Container(
child: Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
height: 353.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.scaleDown,
alignment: FractionalOffset.center,
image: NetworkImage(
fixedTest[index]['urlToImage'])),
),
),
Container(
margin: EdgeInsets.only(top: 285),
child: Padding(
padding: const EdgeInsets.all(10),
child: SizedBox(
// child: new RichText(
// text: new TextSpan(text: 'Non touchable. ', children: [
// new TextSpan(
// text: 'Tap here.',
// recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
// )
// ]),
// )
child: Text(
fixedTest[index]['title'],
style: TextStyle(fontSize: 16),
maxLines: 4,
textAlign: TextAlign.center,
)
),
),
decoration: BoxDecoration(
color: Colors.purple[100],
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.8),
spreadRadius: 1,
blurRadius: 3,
offset: Offset(1, 1),
),
],
),
)
],
),
))),
))
我不明白有什么问题。我找到了关于 addListener(),但我误解了如何使用它。
【问题讨论】:
-
进行测试,将滚动控制器声明移动到 StatefulWidget 构造函数的开头。如果您在构建中声明它,则每次重新构建小部件时都会创建一个新的滚动控制器。
-
错误继续显示
标签: flutter flutter-layout flutter-dependencies