确定滚动小部件的高度 [英] Determine Scroll Widget height

查看:59
本文介绍了确定滚动小部件的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定可滚动"窗口小部件是否确实需要滚动.我最终希望显示类似滚动更多"的内容.我该如何实现?

I would like to determine if a 'scrollable' widget indeed needs to scroll. I would ultimately like to show something like 'Scroll for more'. How do I achieve this?

我可以结合使用LayoutBuilder和ScrollController.但是,ScrollController仅在发生任何事件后才给我maxScrollExtent和minScrollExtent-例如说用户尝试滚动

I could use a combination of LayoutBuilder and a ScrollController. However, ScrollController gives me maxScrollExtent and minScrollExtent only after any event - like say for example user trying to scroll

我想在构建"过程中知道,以便我可以决定是否显示滚动更多",具体取决于屏幕尺寸

I would like to know during 'build' so that I can determine to show 'Scroll for more' or not depending on the screen dimensions

class _MyHomePageState extends State<MyHomePage> {
  int value = 0;
  String text = 'You have pushed the button 0 times\n';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              flex: 9,
              child: SingleChildScrollView(
                child: Text(text),
              ),
            ),
            Expanded(
              flex: 1,
                child: Text('Scroll for more')),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            value += 1;
            text = text + 'You have pushed the button $value times \n';
          });
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 
    );
  }
}

我想动态显示

            Text('Scroll for more'),

,如果需要滚动SingleChildScrollView以查看整个内容.请注意,我仅以上面的代码为例.

only if the SingleChildScrollView needs to be scrolled to view the entire content. Please note, I am just giving the above code as example.

在我的实际情况下,我在SingleChildScrollView中有一个StreamBuilder,但我无法确定将从流中流入多少数据.我也没有任何按钮或点击或任何手势来访问控制器和setState

In my real case, I have a StreamBuilder inside a SingleChildScrollView and I cannot determine how much of data is going to be flowing in from the stream. I also do not have any button or tap or any gesture to access the controller and setState

在此先感谢您的帮助

推荐答案

class _MyHomePageState extends State<MyHomePage> {
  bool showMore = false;
  final scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      setState(() {
        showMore = scrollController.position.maxScrollExtent > 0;
      });
    });
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              flex: 9,
              child: SingleChildScrollView(
                controller: scrollController,
                child: SizedBox(height: 650, child: Text('blah')),
              ),
            ),
            if (showMore) Expanded(flex: 1, child: Text('Scroll for more')),
          ],
        ),
      ),
    );
  }
}

这篇关于确定滚动小部件的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆