简体   繁体   中英

Flutter: ListView not scrolling

Problem:

Initially I have disabled ListView scrolling, and want to enable it after 3 seconds. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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