簡體   English   中英

當滾動條位於列表的底部時,在抖動中啟用按鈕

[英]Enable button in flutter when scrollbar is at the bottom of the list

滾動條位於列表的底部時,誰能發布示例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,
                  ))
            ]);
          }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM