首页 文章

将卡添加到ListView

提问于
浏览
1

我正在尝试获取Cards的列表,并尝试使用 Expanded 小部件,但得到 overflow 错误

我的代码:

new Expanded(
      child: StreamBuilder(
          stream: Firestore.instance.collection('baby').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('Loading...');
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                padding: const EdgeInsets.only(top: 10.0),
                itemExtent: 25.0,
                itemBuilder: (context, index) {
                  DocumentSnapshot ds = snapshot.data.documents[index];
                  return //Text(" ${ds['name']} ${ds['vote']}");
                    Card(
                      child: Expanded(
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            const ListTile(
                              leading: const Icon(Icons.album),
                              title: const Text('The Enchanted Nightingale'),
                              subtitle: const Text('Music by Julie Gable. Lyrics by Sidney Stein.'),
                            ),
                            new ButtonTheme.bar( // make buttons use the appropriate styles for cards
                              child: ButtonBar(
                                children: <Widget>[
                                   FlatButton(
                                    child: const Text('BUY TICKETS'),
                                    onPressed: () { /* ... */ },
                                  ),
                                   FlatButton(
                                    child: const Text('LISTEN'),
                                    onPressed: () { /* ... */ },
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                    ),
                    );
                });
          })),

我得到的错误是: Incorrect use of ParentDataWidget.

完整错误:

进行热重装... I / flutter(9119):══╡由WIDGETS LIBRARY引起的异常╞══════════════════════════════════════════════════════════════════════════════════════════════ ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ englishLike I /扑(9119):body1).merge(blackMountainView body1),继承:假的,颜色:颜色(0xdd000000),家族:的Roboto,I /扑(9119):尺寸:14.0,重量:400,基线:字母,decoration:TextDecoration.none,softWrap:wrapped I / flutter(9119):at box width,overflow:clip):I / flutter(9119):不正确使用ParentDataWidget . I / flutter(9119):扩展的小部件必须直接放在Flex小部件中 . I /扑(9119):扩展(没有深度,弯曲:1,肮脏)具有一个Flex祖先,但它们之间有其它窗口小部件:I /扑(9119): - _InkFeatures- [GlobalKey#93e52墨迹再现器I / flutter(9119): - CustomPaint I / flutter(9119): - PhysicalShape(clipper:ShapeBorderClipper,海拔:1.0,颜色:颜色(0xffffffff),shadowColor:I / flutter(9119):颜色(0xff000000))I / flutter( 9119): - 填充(填充:EdgeInsets.all(4.0))I / flutter(9119): - 语义(容器:true,属性:SemanticsProperties,label:null,value:null,hint:null)I / flutter(9119) ): - RepaintBoundary - [<0>] I / flutter(9119): - KeepAlive(keepAlive:false)I / flutter(9119): - SliverFixedExtentList(委托:SliverChildBuilderDelegate#b334e(估计子计数:3))I / flutter (9119): - SliverPadding(填充:EdgeInsets(0.0,10.0,0.0,0.0))I /扑(9119): - 视口(axisDirection:下来,锚定:0.0,偏移:ScrollPositionWithSingleContext#bebad(偏移:I /扑( 9119):0.0,范围:0.0..0.0,视口: 380.0,ScrollableState,AlwaysScrollableScrollPhysics - > I / flutter(9119):ClampingScrollPhysics,IdleScrollActivity#7b3a8,ScrollDirection.idle))I / flutter(9119): - IgnorePointer- [GlobalKey#4c7f9](忽略:false,ignoringSemantics:false)I / flutter(9119): - 语义(容器:false,属性:SemanticsProperties,label:null,value:null,hint:null)I / flutter(9119): - 监听器(监听器:[向下],行为:不透明)I / flutter(9119): - _GestureSemantics I / flutter(9119): - _ExcludableScrollSemantics- [GlobalKey#22165] I / flutter(9119): - RepaintBoundary I / flutter(9119): - CustomPaint I / flutter(9119): - RepaintBoundary I / flutter(9119): - 扩展(flex:1)(这是与有问题的扩展不同)I / flutter(9119):这些小部件不能介于Expanded和Flex之间 . I / flutter(9119):违规扩展的父级的所有权链是:I / flutter(9119):DefaultTextStyle←AnimatedDefaultTextStyle←_InkFeatures- [GlobalKey#93e52墨水渲染器]←I / flutter(9119):NotificationListener←CustomPaint ←←_ShapeBorderPaint PhysicalShape I /扑(9119):←←_MaterialInterior材料←填充←⋯I /扑(9119):════════════════════════ ══════════════════════════════════════════════════ ═══════════════════════════════════════════════════════════════════════════════════════════════════════I I / chatty(9119):uid = 10096(com.example.flutterapp)Thread-3相同的3行I / flutter(9119):抛出另一个异常:不正确使用ParentDataWidget .

UPDATE
这是我得到的输出屏幕:

enter image description here

如果我删除 Expanded ,输出变为如下:

enter image description here

2 回答

  • 0

    你能在你的问题中放置手机屏幕的印刷品吗?

    我把你的代码放在没有扩展的小部件和它的完美,你想要改变什么?

    Print mobile

  • 1

    我想通了,整个问题是在 ListView.builder 使用 itemExtent: 25.0, 删除它,默认情况下一切都变得可扩展并且运行顺利 .

    在搜索解决方案时,我遇到了thisthis以及this,它帮助我用更干净的代码重建应用程序,下面是对谁感兴趣:

    main.dart

    import 'package:flutter/material.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'BabyModel.dart';
    import 'BabyCard.dart';
    
    void main() => runApp(MyApp(
      textInput: Text("Text Widget"),
    ));
    
    class MyApp extends StatefulWidget {
      final Widget textInput;
      MyApp({this.textInput});
    
      @override
      State<StatefulWidget> createState() => MyAppState();
    }
    
    class MyAppState extends State<MyApp> {
      bool checkBoxValue = false;
    
      @override
      Widget build(BuildContext ctxt) {
        return StreamBuilder(
          stream: Firestore.instance.collection('baby').snapshots(),
          builder: (_, AsyncSnapshot<QuerySnapshot> snapshot) {
            var documents = snapshot.data?.documents ?? [];
            var baby =
            documents.map((snapshot) => BabyData.from(snapshot)).toList();
            return BabyPage(baby);
          },
        );
      }
    }
    
    class BabyPage extends StatefulWidget {
      final List<BabyData> allBaby;
    
      BabyPage(this.allBaby);
    
      @override
      State<StatefulWidget> createState() {
        return BabyPageState();
      }
    }
    
    
    class BabyPageState extends State<BabyPage> {
      @override
      Widget build(BuildContext context) {
    
      //  var filteredBaby = widget.allFish.where((BabyData data) {
      //    data.name = 'Dana';
      //  }).toList();
    
        return MaterialApp(
            home: SafeArea(
            child: Scaffold(
            body: Container(
            child: ListView.builder(
                itemCount: widget.allBaby.length,
                padding: const EdgeInsets.only(top: 10.0),
                itemBuilder: (context, index) {
                  return BabyCard(widget.allBaby[index]);
                })
          ),
        )));
      }
    }
    

    BabyModel.dart

    import 'package:cloud_firestore/cloud_firestore.dart';
    
    class BabyData {
      final DocumentReference reference;
      String name;
      int vote;
    
      BabyData.data(this.reference,
          [this.name,
            this.vote]) {
        // Set these rather than using the default value because Firebase returns
        // null if the value is not specified.
        this.name ??= 'Frank';
        this.vote ??= 7;
      }
    
      factory BabyData.from(DocumentSnapshot document) => BabyData.data(
          document.reference,
          document.data['name'],
          document.data['vote']);
    
      void save() {
        reference.setData(toMap());
      }
    
      Map<String, dynamic> toMap() {
        return {
          'name': name,
          'vote': vote,
        };
      }
    }
    

    BabyCard.dart

    import 'package:flutter/material.dart';
    import 'BabyModel.dart';
    
    class BabyCard extends StatefulWidget {
      final BabyData baby;
    
      BabyCard(this.baby);
    
      @override
      State<StatefulWidget> createState() {
        return BabyCardState(baby);
      }
    }
    
    class BabyCardState extends State<BabyCard> {
      BabyData baby;
      String renderUrl;
    
      BabyCardState(this.baby);
    
      Widget get babyCard {
        return
          new Card(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                ListTile(
                  leading: const Icon(Icons.album),
                  title: Text('The ${baby.name} is having:'),
                  subtitle: Text('${baby.vote} Votes.'),
                ),
                new ButtonTheme.bar( // make buttons use the appropriate styles for cards
                  child: new ButtonBar(
                    children: <Widget>[
                      new FlatButton(
                        child: const Text('Thumb up'),
                        onPressed: () { /* ... */ },
                      ),
                      new FlatButton(
                        child: const Text('Thumb down'),
                        onPressed: () { /* ... */ },
                      )]))]));
      }
    
      @override
      Widget build(BuildContext context) {
        return new Container(
              child:  babyCard,
            );
      }
    }
    

    输出是:

    enter image description here

相关问题