Flutter NestedViewController innerController null错误

问题描述

我正在尝试访问this doc example中所示的nestedViewController的innerController,但是却遇到了null引用异常(innerController调用为null)。任何人都可以对此有所了解吗?

class ChatTest extends StatelessWidget {
  ChatTest({
    Key key
  }): super(key: key);
  ScrollController innerScrollController;
  final GlobalKey < nestedScrollViewState > globalKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: nestedScrollView(
        key: globalKey,floatHeaderSlivers: false,headerSliverBuilder: (BuildContext context,bool innerBoxIsScrolled) {
          return <Widget > [
            SliverAppBar(
              title: const Text('Floating nested SliverAppBar'),floating: true,expandedHeight: 200.0,actions: [
                  FlatButton(onpressed: () => gotoBottom(),child: Text('ok'))
                ],),];
        },body: Flexible(
          child: ListView.builder(
            controller: innerController,padding: const EdgeInsets.all(8),itemCount: 30,reverse: true,itemBuilder: (BuildContext context,int index) {
                return Container(
                  height: 50,child: Center(child: Text('Item $index')),);
              }),);
  }

  ScrollController get innerController {
    return globalKey.currentState.innerController;
  }

  gotoBottom() {
    innerController.animateto(0.0,duration: Duration(milliseconds: 300),curve: Curves.eaSEOut);
  }
}

它基本上是用于带有可折叠标题的聊天屏幕。每次添加新消息时,我都希望滚动到底部

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)