带有标题的 Flutter GridView

Posted

技术标签:

【中文标题】带有标题的 Flutter GridView【英文标题】:Flutter GridView with Header 【发布时间】:2019-09-25 18:33:41 【问题描述】:

如何在 FLUTTER 中创建带有可滚动工具栏的网格视图。我找到了带有标题ListViewWithHeader 的列表视图。但是我需要下面提到的带有标题的 GridView。我已经使用 SilverGrid 忽略器来构建这个布局,这有点滞后。所以我需要另一种选择。

Container setDataToContainer() 
  return Container(
    color: Colors.white,
    margin: EdgeInsets.only(left: 4.0, right: 4, bottom: 4, top: 4),
    child: CustomScrollView(
      slivers: <Widget>[
        SliverList(
          delegate: SliverChildListDelegate(
            [
              HeaderWidget("Header 1"),
            ],
          ),
        ),
        SliverGrid(
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          delegate: SliverChildListDelegate(
            [
              BodyWidget("title", "key_x", "pnl.svg"),
              BodyWidget("title2", "key_x", "cls.svg"),
              BodyWidget(
                  "title3", "key_x", "irr.svg"),
              BodyWidget(
                  "title4", "key_x", "icp.svg"),
            ],
          ),
        ),
        SliverList(
          delegate: SliverChildListDelegate(
            [
              HeaderWidget("Header 2"),
            ],
          ),
        ),
        SliverGrid(
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          delegate: SliverChildListDelegate(
            [
              BodyWidget("title5", "key_x", "ict.svg"),
              BodyWidget("title6", "key_x", "icc.svg"),
            ],
          ),
        ),
        SliverList(
          delegate: SliverChildListDelegate(
            [
              HeaderWidget("Others"),
            ],
          ),
        ),
        SliverGrid(
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          delegate: SliverChildListDelegate(
            [
              BodyWidget("title7", "key_x", "icd.svg"),
              BodyWidget("title8", "6", "ici.svg"),
            ],
          ),
        ),
      ],
    ),
  );


class HeaderWidget extends StatelessWidget 
  final String text;

  HeaderWidget(this.text);

  @override
  Widget build(BuildContext context) 
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10, bottom: 2, top: 20),
      child: Text(
        text.toUpperCase(),
        style: TextStyle(
            color: hexToColor(themeColor1),
            fontSize: 16,
            fontWeight: FontWeight.bold),
      ),
      color: Colors.white,
    );
  


class BodyWidget extends StatelessWidget 
  final String imagePath;
  final String title;
  final String navigationKey;

  BodyWidget(this.title, this.navigationKey, this.imagePath);

  @override
  Widget build(BuildContext context) 
    return Container(
        alignment: Alignment.center,
        child: Card(
          color: hexToColor(themeColor1),
          elevation: 5,
          child: InkWell(
            onTap: () 
              navigateToView(context, navigationKey);
            ,
            child: Stack(
              children: <Widget>[
                Align(
                  alignment: Alignment.topCenter,
                  child: Container(
                    margin: EdgeInsets.only(top: 40),
                    child: SvgPicture.asset(
                      "assets/images/$imagePath",
                      color: Colors.white,
                      width: 35,
                      height: 35,
                    ),
                  ),
                ),
                Align(
                  alignment: Alignment.bottomCenter,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    verticalDirection: VerticalDirection.up,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.all(10),
                        color: Colors.black.withOpacity(.2),
                        child: Text(
                          title,
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                              fontWeight: FontWeight.normal),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ));
  

  void navigateToView(BuildContext context, String navigationKey) 

    Navigator.push(
      context,
      PageRouteBuilder(
        pageBuilder: (context, animation1, animation2) 
          return NewSections();
        ,
        transitionsBuilder: (context, animation1, animation2, child) 
          return FadeTransition(
            opacity: animation1,
            child: child,
          );
        ,
        transitionDuration: Duration(milliseconds: 600),
      ),
    );
  

【问题讨论】:

你为此尝试了 sticky_headers 插件 不,我没有。我想用另一个选项替换 SliverGrid,因为在我的情况下它有点滞后。 嗨@jazzbpn,我面临同样的问题。你找到解决办法了吗? 您可以使用SliverToBoxAdapter 而不是SliverList 与一个孩子一起编写标题文本! 【参考方案1】:

你可以试试这个代码而不是 SliverGrid

sticky_headers: ^0.1.7

将上述插件添加到 pubspec.yaml 中

import 'package:flutter/material.dart';
import 'package:sticky_headers/sticky_headers.dart';

class GridHeader extends StatefulWidget 
  @override
  _GridHeaderState createState() => _GridHeaderState();


class _GridHeaderState extends State<GridHeader> 

  List<String> listHeader = ['HEADER1','HEADER2','HEADER3','HEADER4','HEADER5','HEADER6','HEADER7','HEADER8','HEADER9','HEADER10',];
  List<String> listTitle = ['title1','title2','title3','title4',];

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text("Grid Header Demo"),
      ),
      body: gridHeader(),
    );
  


  Widget gridHeader()
    return new ListView.builder(itemCount: listHeader.length,itemBuilder: (context, index) 
      return new StickyHeader(
        header: new Container(
          height: 38.0,
          color: Colors.white,
          padding: new EdgeInsets.symmetric(horizontal: 12.0),
          alignment: Alignment.centerLeft,
          child: new Text(listHeader[index],
            style: const TextStyle(color: Colors.purple, fontSize: 20,fontWeight: FontWeight.bold),
          ),
        ),
        content: Container(
          child: GridView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: listTitle.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2,childAspectRatio: 1,),
            itemBuilder: (contxt, indx)
              return Card(
                margin: EdgeInsets.all(4.0),
                color: Colors.purpleAccent,
                child: Padding(
                  padding: const EdgeInsets.only(left: 12.0, top: 6.0, bottom: 2.0),
                  child: Center(child: Text(listTitle[indx], style: TextStyle(fontSize: 14, color: Colors.black54),)),
                ),
              );
            ,
          ),
        ),
      );
    ,
      shrinkWrap: true,
    );
  

根据您的要求更改您的 listHeaderlistTitle 数据

【讨论】:

每次滚动时图像都会重新加载。我该如何处理? 本地,来自资产文件夹。 如果你正在使用像CachedNetworkImage和precacheImage,然后尝试Image.asset(“”)本地图像 是的,我试过了,但它仍然滞后。每次我滚动项目时都会一次又一次地重新加载。

以上是关于带有标题的 Flutter GridView的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - 如何让带有孩子的容器占据整个屏幕

带有搜索栏的 Flutter 粘性标题

Flutter:带有初始网络请求的 ChangeNotifierProvider

Flutter - 选择带有显示和返回值的列表

带有圆角的 Flutter WebView

Flutter:带有hintText的Cupertino TextField