[英]Flutter: ListView not scrolling
問題:
最初我禁用了ListView
滾動,並希望在 3 秒后啟用它。 當應用程序啟動並且您繼續滾動它 5 秒鍾(沒有將手指從屏幕上移開)時, ListView
不會滾動。
但是它應該已經滾動,因為我在第 3 秒啟用滾動,控制台確認ListView enabled
,但我仍然無法滾動它。
代碼:
bool _enabled = false; // scrolling disabled initially
@override
void initState() {
super.initState();
Timer(Duration(seconds: 3), () {
print("Scrolling enabled");
setState(() => _enabled = true); // scrolling enabled after 3 seconds
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
physics: _enabled ? ClampingScrollPhysics() : NeverScrollableScrollPhysics(),
itemBuilder: (_, i) => ListTile(title: Text("Item $i")),
),
);
}
試試這個...
class _blabla extends State<blabla> {
Timer _timer;
}
@override
void initState() {
super.initState();
bool _enabled = false;
);
}
_blablaState() {
_timer = new Timer(const Duration(seconds: 3), () {
setState(() => _enabled = true);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
physics: _enabled ? ClampingScrollPhysics() : NeverScrollableScrollPhysics(),
itemBuilder: (_, i) => ListTile(title: Text("Item $i")),
),
);
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
我也會嘗試禁用物理,看看它是否有所作為。 可能會發生沖突。
這是一個解決方法:
final _scrollController = ScrollController();
var _firstScroll = true;
bool _enabled = false;
@override
void initState() {
super.initState();
Timer(Duration(seconds: 3), () {
setState(() => _enabled = true);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onVerticalDragUpdate: (details) {
if (_enabled && _firstScroll) {
_scrollController
.jumpTo(_scrollController.position.pixels - details.delta.dy);
}
},
onVerticalDragEnd: (_) {
if (_enabled) _firstScroll = false;
},
child: AbsorbPointer(
absorbing: !_enabled,
child: ListView.builder(
controller: _scrollController,
physics: ClampingScrollPhysics(),
itemBuilder: (_, i) => ListTile(title: Text("Item $i")),
),
),
),
);
}
嗨,親愛的,您的代碼正在我的設備中運行,這是我的代碼,幾乎相同。
如果對您有用,請告訴我,否則我將其刪除
bool _enabled = false; // scrolling disabled initially
@override
void initState() {
super.initState();
Timer(Duration(seconds: 15), () {
print("Scrolling enabled");
setState(() =>
_enabled = true
); // scrolling enabled after 3 seconds
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 30,
physics: _enabled ? ClampingScrollPhysics() : NeverScrollableScrollPhysics(),
itemBuilder: (_, i) => ListTile(title: Text("Item $i")),
),
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.