【问题标题】:Flutter Displaying a gridview inside a listviewFlutter 在列表视图中显示网格视图
【发布时间】:2018-12-21 07:41:18
【问题描述】:

我正在尝试在网格视图上方显示搜索栏(来自流构建器)。我希望搜索栏与 gridview 一起向上滚动。我尝试了多种方法将它们包装在扩展/灵活的小部件中,但我总是以错误告终。有人处理过这个吗?

这是我现在拥有的,它会滚动但搜索栏保持在顶部。

new Column(
    children: <Widget>[
      new Flexible(
        child: new Column(
          children: <Widget>[
            new Container(
              margin: new EdgeInsets.only(left: 4.0, right: 4.0),
              color: Colors.white,
              child: new Row(
                children: <Widget>[
                  new Container(
                    margin: new EdgeInsets.only(left: 8.0, right: 8.0, top: 8.0, bottom: 8.0),
                    child: Icon(Icons.search),
                  ),
                  new Container(
                      child: Flexible(
                        child: new TextField(
                          onChanged: (String text) {
                            setState(() {
                              _searchController.text = text;
                            });
                          },
                          decoration: new InputDecoration.collapsed(hintText: 'Search...'),
                          controller: _searchController,
                        ),
                      )
                  ),
                ],
              ),
            ),
            new Flexible(
                child: new StreamBuilder(
                    stream: stgDocument.collection('people').orderBy('scroll').snapshots(),
                    builder: (context, snapshot) {
                      if (!snapshot.hasData) return const Text('Loading...');
                      _getMembers(snapshot);
                      return new GridView.builder(
                        //itemCount: snapshot.data.documents.length,
                        itemCount: _searchedMemberList.length,
                        gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
                        itemBuilder: (context, index) =>
                            _buildPeopleCards(context, _searchedMemberList[index]),
                        //_buildPeopleCards(context, snapshot.data.documents[index])
                      );
                    }
                )
            ),
          ],
        ),
      ),
    ],
  )

【问题讨论】:

    标签: listview flutter scrollable flutter-layout


    【解决方案1】:

    您可以创建具有多个子级的可滚动ListView,其中一个是GridView,其结构如下。要为ListView中的所有元素启用滚动,您需要将其参数physics设置为NeverScrollablePhysics

     - ListView
       - Container // your searchbar would go here
       - GridView  // physics: NeverScrollablePhysics
         - Children
    

    这是一个完整的代码示例:

    import 'package:flutter/material.dart';
    import 'package:cached_network_image/cached_network_image.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(),
          home: Scaffold(
            body: ListView(
              children: <Widget>[
                Container(
                  color: Colors.red,
                  height: 50.0,
                ),
                GridView.count(
                  physics: NeverScrollableScrollPhysics(),
                  crossAxisCount: 3,
                  shrinkWrap: true,
                  children: generateNumbers().map((int i) {
                    return Text(i.toString());
                  }).toList(),
                )
              ],
            ),
          ),
        );
      }
    
      List<int> generateNumbers() => List<int>.generate(30, (i) => i + 1);
    }
    

    结果如下:

    【讨论】:

    • @Alvindrakes 总是乐于提供帮助。特别是在颤振社区:)
    • 非常感谢。一直卡在这上面。
    猜你喜欢
    • 2019-06-01
    • 2011-07-13
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多