简体   繁体   English

如何使用网格 Flutter 同时滚动小部件

[英]How to scroll widget in same time with grid Flutter

How I can scroll widget in same time我如何同时滚动小部件

Flutter Flutter

Screenshot截屏

Image图片

Here is code for this screen这是此屏幕的代码

return Scaffold(
    appBar: CustomAppBar(
      scaffoldKey: widget.scaffoldKey,
      // icon: AppIcon.settings,
      // onActionPressed: onSettingIconPressed,
      onSearchChanged: (text) {
        setState(() {
          if (text.length > 0) {
            searching = true;
          } else {
            searching = false;
          }
        });

        state.filterByUsername(text);
      },
    ),
    backgroundColor: Colors.white,
    body: new RefreshIndicator(
        backgroundColor: Colors.white,
        onRefresh: () async {
          HapticFeedback.selectionClick();
          setState(() {
            list3 = state2.getPostList(authstate.userModel);
            list3.shuffle();
            onlyImages.shuffle();
          });
          state.getDataFromDatabase();
          return Future.value(true);
        },

        // mesto povise greed, i ispod search boxa

        child: new Column(
          children: <Widget>[
            searching
                ? ListView.separated(
                    addAutomaticKeepAlives: false,
                    physics: BouncingScrollPhysics(),
                    itemBuilder: (context, index) =>
                        _UserTile(user: list[index]),
                    separatorBuilder: (_, index) => Divider(
                      color: Colors.orange,
                      height: 0,
                    ),
                    itemCount: list?.length ?? 0,
                  )
                : slider(),
            searching
                ? ListView.separated(
                    addAutomaticKeepAlives: false,
                    physics: BouncingScrollPhysics(),
                    itemBuilder: (context, index) =>
                        _UserTile(user: list[index]),
                    separatorBuilder: (_, index) => Divider(
                      color: Colors.orange,
                      height: 0,
                    ),
                    itemCount: list?.length ?? 0,
                  )
                : Container(
                    height: 32,
                    margin: EdgeInsets.only(top: 5, bottom: 5),
                    child: ListView(
                        scrollDirection: Axis.horizontal,
                        children: <Widget>[
                          _tagItem(Icon(Icons.dehaze, color: Colors.orange),
                              'DAJGI', null, Colors.black, () {
                            Navigator.pushNamed(
                                context, '/PhoneConfirmCode');
                          }),
                          _tagItem(null, 'Nature', null,
                              Colors.greenAccent.withOpacity(0.3), null),
                          _tagItem(
                              null, 'Animal', null, Colors.brown, null),
                          _tagItem(null, 'Travel', null, Colors.teal, null),
                          _tagItem(
                              null, 'Happy', null, Colors.orange, null),
                          _tagItem(null, 'Art', null, Colors.blue, null),
                          _tagItem(
                              null, 'Night', null, Colors.blueGrey, null),
                        ]),
                  ),

            // Grid List
            _listOfPosts(),

            // Expanded(
            //   child: GridView.count(
            //     crossAxisCount: 3,
            //     children: List.generate(onlyImages.length, (index) {
            //       return GestureDetector(
            //           onTap: () {
            //             FeedModel model = onlyImages[index];
            //             onPostPressed(context, model);
            //           },
            //           // onLongPress: () {
            //           //   _createPopupContext;
            //           //   FeedModel model = onlyImages[index];
            //           //   onPostPressed(context, model);
            //           // },
            //           child: Container(
            //             child: Card(
            //                 child: onlyImages
            //                             .elementAt(index)
            //                             .imagesPath
            //                             .length >
            //                         0
            //                     ? CachedNetworkImage(
            //                         imageUrl: onlyImages
            //                             .elementAt(index)
            //                             .imagesPath[0],
            //                         fit: BoxFit.cover,
            //                       )
            //                     :
            //                     // Container()
            //                     Center(
            //                         child: Text(onlyImages
            //                             .elementAt(index)
            //                             .description),
            //                       )),
            //           ));
            //     }
            //     ),
            //   ),
            // ),
          ],
        )));

When I starting to scroll now only grid scrolling but widget not moving, and I want that this widget going up when user starting to scroll.. Can anyone tell me what's the problem and how to fix it?当我现在开始滚动时,只有网格滚动但小部件不动,我希望当用户开始滚动时这个小部件会上升。谁能告诉我问题出在哪里以及如何解决?

Updated the full code as requested in the comments.按照评论中的要求更新了完整的代码。

First of all, Column is un-scrollable, so to make widgets scrollable is to wrap those widgets with a ListView instead of a Column .首先, Column是不可滚动的,因此要使小部件可滚动是用ListView而不是Column包装这些小部件。 So you can do something like this to make widgets scrollable:所以你可以做这样的事情来使小部件可滚动:

RefreshIndicator(
    backgroundColor: Colors.white,
    onRefresh: (_){},
    child: new ListView(
      children: <Widget>[
        // widget 1,
        // widget 2,
        // widget 3,
      ],
),

If ListView make widgets scrollable, what happens when a ListView is wrap in another ListView ?如果ListView使小部件可滚动,当ListView包装在另一个ListView中时会发生什么? A scrollable inside a scrollable?可滚动内的可滚动? (Well there are some cases when you want to do this, but thats not the case here so yeah). (嗯,有些情况下你想这样做,但这里不是这样,所以是的)。 There will be error.会有错误。

So to avoid the error, just add shrinkWrap: true and physics: ClampingScrollPhysics() and you're good to go, as such:所以为了避免错误,只需添加shrinkWrap: truephysics: ClampingScrollPhysics()你就可以使用go,如下所示:

RefreshIndicator(
  backgroundColor: Colors.white,
  onRefresh: (_){},
  child: new ListView(
    children: <Widget>[
      // listview
      ListView.separated(
        shrinkWrap: true,                 // <--- add this
        physics: ClampingScrollPhysics(), // <--- and this
        ...
      ),
      // gridview
      GridView.builder(
        shrinkWrap: true,                 // <--- add this
        physics: ClampingScrollPhysics(), // <--- and this
        ...
      ),
    ],
  ),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM