【问题标题】:Unable to keep other widgets fixed and Listview.builder scroll in flutter无法保持其他小部件固定,Listview.builder 滚动滚动
【发布时间】:2021-11-04 22:51:54
【问题描述】:

我想保持列表视图构建器上方的容器固定,并让列表视图滚动。但是当我实现以下代码时,它显示了 renderflex 溢出问题。

代码:

List<String> companynames = [
    'Delloite',
    'Google',
    'Amazon',
    'Apple',
    'Tata',
    'Wipro',
    'Tesla',
    'Cupid',
    'Pfizer',
    'Mango',
    'Reliance',
    'Titan',
    'Toyota',
    'Maruti',
    'Volkswagen',
    'Suzuki'
  ];
Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: Column(
        //mainAxisSize: MainAxisSize.max,
        children: [
          Stack(
            children: <Widget>[
              Container(
                color: kbluePrimaryColor,
                height: MediaQuery.of(context).size.height * 0.2,
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(
                    left: 20.0,
                    right: 20.0,
                    top: MediaQuery.of(context).size.height * 0.12,
                  ),
                  child: ListView.builder(
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      //physics: NeverScrollableScrollPhysics(),
                      itemCount: companynames.length,
                      itemBuilder: (context, index) {
                        return Card(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(15))),
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(
                              companynames[index],
                              style: TextStyle(
                                color: kblackPrimaryColor,
                                fontSize:
                                    MediaQuery.of(context).size.width * 0.06,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        );
                      }),
                ),
              )
            ],
          ),
        ],
      ),
    );
  }

我希望蓝色容器固定,列表视图的第一项与蓝色容器重叠一半并且可滚动。有人能告诉我怎么做吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    请试试这个

     List<String> companynames = [
        'Delloite',
        'Google',
        'Amazon',
        'Apple',
        'Tata',
        'Wipro',
        'Tesla',
        'Cupid',
        'Pfizer',
        'Mango',
        'Reliance',
        'Titan',
        'Toyota',
        'Maruti',
        'Volkswagen',
        'Suzuki'
      ];
      Widget build(BuildContext context) {
        // TODO: implement build
        return Scaffold(
          body: Stack(
            children: <Widget>[
              Container(
                color: Colors.green,
                height: MediaQuery.of(context).size.height * 0.2,
              ),
              Padding(
                padding: EdgeInsets.only(
                  left: 20.0,
                  right: 20.0,
                  top: MediaQuery.of(context).size.height * 0.12,
                ),
                child: ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    //physics: NeverScrollableScrollPhysics(),
                    itemCount: companynames.length,
                    itemBuilder: (context, index) {
                      return Card(
                        color: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius:
                            BorderRadius.all(Radius.circular(15))),
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Text(
                            companynames[index],
                            style: TextStyle(
                              //  color: kblackPrimaryColor,
                              fontSize:
                              MediaQuery.of(context).size.width * 0.06,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      );
                    }),
              )
            ],
          ),
        );
      }
    

    输出:

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2015-12-23
      • 2017-08-07
      • 2013-07-31
      相关资源
      最近更新 更多