颤振:ListView不滚动

时间:2019-10-14 15:28:04

标签: flutter dart flutter-layout

问题:

最初,我已禁用ListView滚动,并希望在3秒钟后启用它。应用启动后,您不断滚动它大约5秒钟(而无需离开屏幕),ListView不会滚动。

无论如何它应该已经滚动,因为我在第三秒启用了滚动,控制台确认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")),
    ),
  );
}

2 个答案:

答案 0 :(得分:0)

尝试一下...

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();
}

我也将尝试禁用物理,看看是否有作用。可能有冲突。

答案 1 :(得分:-1)

这是一种解决方法:

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")),
        ),
      ),
    ),
  );
}