【问题标题】:Adding a button to the bottom of a listview widget in flutter在颤动中将按钮添加到列表视图小部件的底部
【发布时间】:2019-03-03 09:07:43
【问题描述】:

我目前在整个屏幕上运行一个列表视图。我想在屏幕底部有一个按钮,从而将其拆分,这样列表视图就不会填满我的整个窗口。

这是当前构建类的代码:

@override
Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Text('HT scoreboard'),
  ),
  body: _buildBody(context),
);
}

Widget _buildBody(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection('Spillere').orderBy("score", descending: true).snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return LinearProgressIndicator();

    return _buildList(context, snapshot.data.documents);
  },
);
}

  Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
return ListView(
  padding: const EdgeInsets.only(top: 10.0),
  children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);
}

Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
final record = Record.fromSnapshot(data);

return Padding(
  key: ValueKey(record.name),
  padding: const EdgeInsets.all(5.0),
  child: Container(
    decoration: BoxDecoration(
      border: Border.all(color: Colors.grey),
      borderRadius: BorderRadius.circular(5.0),
    ),
    child: ListTile(
      title: Text(record.name + ": " + record.score.toString()),
      trailing: new IconButton(icon: new Icon(isAdmin ? Icons.add : null, color: Colors.green),
          onPressed: (){
            if(isAdmin){
              record.reference.updateData({'score': record.score + 1});
            }
          }
      ),
    ),
  ),
);

【问题讨论】:

  • 如果答案正确,请将答案标记为正确:)
  • 我还没有到过我的桌面,但我当然会

标签: dart flutter


【解决方案1】:

更改您的 buildlist 函数以包含一个带有按钮和列表视图的列作为子项

Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
    return Column(
        children:[
            Expanded(
            child: ListView(
                   padding: const EdgeInsets.only(top: 10.0),
                   children: snapshot.map((data) => _buildListItem(context, data)).toList(),
                   ),
            ),
            RaisedButton(
            // fill in required params
            )
         ])
}

【讨论】:

  • 它也解决了我的问题。但是这种方法导致了一个新问题:每当任何输入字段获得焦点并且键盘打开时,该提升按钮就会被键盘向上推,有什么解决方案可以防止这种情况发生吗?
  • @HarshJatinder 你找到解决办法了吗?
  • 我实际上遇到了同样的问题。如果有人对此有任何解决方案,我会很高兴知道。谢谢
  • 你可以在脚手架上使用 resizeToAvoidBottomInset : false 属性来避免小部件被键盘向上推
【解决方案2】:

为了防止按钮被推到键盘上方;

return CustomScrollView(
  slivers: <Widget>[
    SliverToBoxAdapter(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          // list items
        ],
      ),
    ),
    SliverFillRemaining(
      hasScrollBody: false,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          RaisedButton()
        ],
      ),
    )
  ],
);

【讨论】:

  • 好人,谢谢分享。我有几天遇到这个问题。
  • 这是唯一有效的答案,非常感谢)
  • 多么漂亮的解决方案!很整洁!
猜你喜欢
  • 2020-02-05
  • 2011-08-20
  • 2020-08-09
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
相关资源
最近更新 更多