滚动条位于列表的底部时,谁能发布示例Flutter代码来启用按钮?

我使用了NotificationListener和NotificationListener,但是当滚动条位于列表底部时,事件未触发。

我只想在滚动条位于列表底部时启用该按钮,否则应将其禁用。

下面是我正在使用的代码。

class _TermsAndCondnsPage extends State<TermsAndCondnsPage> {
bool _isButtonEnabled = false;

bool _scrollingStarted() {
setState(() => _isButtonEnabled = false);
return false;
}

bool _scrollingEnded() {
setState(() => _isButtonEnabled = true);
return true;
}

ScrollController scrollcontroller;
@override
Widget build(BuildContext context) {
var acceptAndContinueButtonPressed;
if (_isButtonEnabled) {
  acceptAndContinueButtonPressed = () {};
} else {
  acceptAndContinueButtonPressed == null;
}
return new Scaffold(
  appBar: new AppBar(
    automaticallyImplyLeading: false,
    title: new Text('Terms And Conditions', textAlign: TextAlign.center),
  ),
  body:
      NotificationListener<ScrollStartNotification>(
          onNotification: (_) => _scrollingStarted(),
          child: NotificationListener<ScrollEndNotification>(
            onNotification: (_) => _scrollingEnded(),
            child: new MaterialApp(
              home: new Scaffold(
                body: new SingleChildScrollView(
                  controller: scrollcontroller,
                  child: new Column(
                    children: <Widget>[
                      new Center(
                        child: new HtmlView(
                          data: html,
                        ),
                      ),
                    ],
                  ),
                ),
                persistentFooterButtons: <Widget>[
                  new RaisedButton(
                      color: Colors.blue,
                      onPressed: _isButtonEnabled ? () {} : null,
                      child: new Text(
                        'Accept and Continue',
                        style: new TextStyle(
                          color: Colors.white,
                        ),
                      )),
                ],
              ),
            ),
          )),
);
}

String html = '''
<p>Sample HTML</p>
''';
}

最佳答案

这里有一个基本示例,到达底部时启用按钮,到达顶部时禁用。

        class ExampleState extends State<Example> {
          final list = List.generate((40), (val) => "val $val");
          final ScrollController _controller = new ScrollController();
          var reachEnd = false;

          _listener() {
            final maxScroll = _controller.position.maxScrollExtent;
            final minScroll = _controller.position.minScrollExtent;
            if (_controller.offset >= maxScroll) {
              setState(() {
                reachEnd = true;
              });
            }

            if (_controller.offset <= minScroll) {
              setState(() {
                reachEnd = false;
              });
            }
          }

          @override
          void initState() {
            _controller.addListener(_listener);
            super.initState();
          }

          @override
          void dispose() {
            _controller.removeListener(_listener);
            _controller.dispose();
            super.dispose();
          }

          @override
          Widget build(BuildContext context) {
            return Stack(children: [
              Positioned(
                top: 0.0,
                bottom: 50.0,
                width: MediaQuery.of(context).size.width,
                child: ListView.builder(
                  controller: _controller,
                  itemBuilder: (context, index) {
                    return ListTile(
                      title: Text(list[index]),
                    );
                  },
                  itemCount: list.length,
                ),
              ),
              Align(
                  alignment: Alignment.bottomCenter,
                  child: RaisedButton(
                    child: Text("button"),
                    color: Colors.blue,
                    onPressed: reachEnd ? () => null : null,
                  ))
            ]);
          }
        }

10-04 17:36