简体   繁体   English

Flutter:ListView 不滚动

[英]Flutter: ListView not scrolling

Problem:问题:

Initially I have disabled ListView scrolling, and want to enable it after 3 seconds.最初我禁用了ListView滚动,并希望在 3 秒后启用它。 The moment app launches and you keep scrolling it for like 5 seconds ( without lifting your finger off the screen ), the ListView doesn't scroll.当应用程序启动并且您继续滚动它 5 秒钟(没有将手指从屏幕上移开)时, ListView不会滚动。

However it should have scrolled because I am enabling scrolling at 3rd second, the console confirms ListView enabled but still I am not able to scroll it.但是它应该已经滚动,因为我在第 3 秒启用滚动,控制台确认ListView enabled ,但我仍然无法滚动它。

Code:代码:

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

try this out...试试这个...

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

I would also try with physics disabled to see if it makes a difference.我也会尝试禁用物理,看看它是否有所作为。 There may be a conflict.可能会发生冲突。

Here is a workaround for this:这是一个解决方法:

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

Hi dear your code is working in my device, here is my code which is almost same.嗨,亲爱的,您的代码正在我的设备中运行,这是我的代码,几乎相同。
If is working for you just tell me otherwise I remove it如果对您有用,请告诉我,否则我将其删除

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM