简体   繁体   English

Flutter - 使用 Column/Expanded/ListView 的布局问题

[英]Flutter - Layout problem using Column/Expanded/ListView

I'm pretty new to Flutter and I can't seem to solve this issue.我是 Flutter 的新手,我似乎无法解决这个问题。

I want to display a ListView of my custom card-like Ink containers and have pretty much achieved it.我想显示我的自定义卡片式 Ink 容器的 ListView 并且几乎已经实现了它。 However, the ListView extends behind my Text widget (the previous item in the column) when I scroll down:但是,当我向下滚动时,ListView 会延伸到我的文本小部件(列中的前一项)后面:

Expanded widget behind text widget文本小部件后面的扩展小部件

Look behind the "Gallery" text when scrolling: Animated GIF showing undesired behavior滚动时查看“画廊”文本后面:动画 GIF 显示不良行为

I would like my ListView widget to scroll without the containers displaying behind the "Gallery" text.我希望我的 ListView 小部件滚动时没有容器显示在“画廊”文本后面。

I have tried without an Expanded widget (ListView.builder() directly under the Column children) but I get this error message in the Debug console:我试过没有扩展小部件(ListView.builder() 直接在 Column 子项下),但我在调试控制台中收到此错误消息:

════════ Exception caught by rendering library ═════════════════════════════════ RenderBox was not laid out: RenderRepaintBoundary#8dc77 relayoutBoundary=up8 NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1702 pos 12: 'hasSize' The relevant error-causing widget was Column ════════渲染库捕获异常═══════════════════════════════RenderBox was not layed═ out: RenderRepaintBoundary#8dc77 relayoutBoundary=up8 NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1702 pos 12: 'hasSize' 导致错误的相关小部件是 Column

And the list does not display at all.并且列表根本不显示。

Here's my code:这是我的代码:

class Gallery extends StatelessWidget {

  final List<_ChallengeCard> challengestest = [
    _ChallengeCard(title: 'Challenge #1', subtitle: 'Completed on 13/11/1978'),
    _ChallengeCard(title: 'Challenge #2', subtitle: 'Completed on 12/18/2043'),
    _ChallengeCard(title: 'Challenge #3', subtitle: 'Current challenge'),
    _ChallengeCard(title: 'Challenge #4', subtitle: 'Locked'),
    _ChallengeCard(title: 'Challenge #5', subtitle: 'Locked'),
    _ChallengeCard(title: 'Challenge #6', subtitle: 'Locked'),
    _ChallengeCard(title: 'Challenge #7', subtitle: 'Locked'),
    ];

    Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: [
                Text(
                'Gallery',
                style: GoogleFonts.alexBrush(fontSize: 55),
                textAlign: TextAlign.center,
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: challengestest.length,
                    itemBuilder: (context, index){
                    return Container(
                      padding: EdgeInsets.symmetric(vertical: 8.0),
                      child: InkWell(
                        onTap: (){},
                        child: challengestest[index]
                      ),
                    );
                    },
                  ),
                ),
              ],
            ),
        );
      }
    }

And some additionnal classes so that you can see the specific content of the ListView item (but I doubt they have anything to do with the issue):还有一些额外的类,这样你就可以看到 ListView 项目的具体内容(但我怀疑它们与这个问题有什么关系):

class _ChallengeCard extends StatelessWidget {
  _ChallengeCard({
    Key key,
    this.title,
    this.subtitle,
  }) : super(key: key);

  final String title;
  final String subtitle;

  @override
  Widget build(BuildContext context) {
    return Ink(
      padding: EdgeInsets.all(11.0),
      decoration: BoxDecoration(
        color: Color(0xFF11001A),
        borderRadius: BorderRadius.all(Radius.circular(10.0))),
      child: Container(
        alignment: Alignment.topLeft,
        child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: Icon(Icons.star,color: Colors.white, size: 40.0,),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children:<Widget>[
              Text(
                '$title',
                style: GoogleFonts.lora(fontSize: 25),
              ),
              Text(
                '$subtitle',
                style: GoogleFonts.lora(fontSize: 15),
              ),
            ],
          ),
        ],
        ),
      ),
    );
  }
}

The class "gallery" is called from the main.dart file (which is why there is a bottom navigation bar in the whole app), but I haven't included it since it is not relevant to the issue. class“画廊”是从 main.dart 文件调用的(这就是为什么整个应用程序中有一个底部导航栏的原因),但我没有包含它,因为它与问题无关。

Thanks in advance!提前致谢!

You can make your text scroll also by wrapping your widget in a single child scroll view您也可以通过将小部件包装在单个子滚动视图中来使文本滚动

   ...

   SingleChildScrollView(// add a scrollview
                child: 
              Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    Text(
                    'Gallery',
                 style: GoogleFonts.alexBrush(fontSize: 55),
                    textAlign: TextAlign.center,
                    ), 
                    //remove the expanded widget
                  ListView.builder(
                    shrinkWrap: true, // make shrikwrap true
                        physics: NeverScrollableScrollPhysics(), // add this physics so it won't scroll
                        itemCount: challengestest.length,
                        itemBuilder: (context, index){
                        return Container(
                        ... // rest of your code here
);}
),
]))

I finally got it to work by using the AppBar widget instead of a Text widget for the "Gallery" title.我终于通过使用 AppBar 小部件而不是“画廊”标题的文本小部件来让它工作。

The Scaffold does not contain a Column widget anymore, only a ListView.Builder: Scaffold 不再包含 Column 小部件,仅包含 ListView.Builder:

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
                shrinkWrap: true,
                itemCount: challengestest.length,
                itemBuilder: (context, index){
                  return Container(
                    padding: EdgeInsets.symmetric(vertical: 8.0),
                    child: InkWell(
                      onTap: (){},
                      child: challengestest[index]
                    ),
                  );
                },
              ),
    );
  }

The text "Gallery" has been moved as a heavily modified AppBar with a Stack widget to retain its appearance:文本“Gallery”已被移动为经过大量修改的 AppBar,带有 Stack 小部件以保留其外观:

   Stack(
      children: <Widget>[
        Image.asset(
            "assets/images/bgpurple.jpeg",
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            fit: BoxFit.cover,
          ),
        Scaffold(
          appBar: PreferredSize(
            preferredSize: Size.fromHeight(85.0),
            child: AppBar(
              automaticallyImplyLeading: false,
              elevation: 0.0,
              backgroundColor: Colors.transparent,
              centerTitle: true,
              flexibleSpace: Padding(
                padding: const EdgeInsets.only(top: 60.0),
                child: Center(
                  child: Text(
                    _myPageTitles[_selectedIndex],
                    style: GoogleFonts.alexBrush(
                      fontSize: 45, 
                      color: Colors.white,
                      )
                    ),
                ),
              ),
            ),
          ),

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

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