Flutter如何遍历地图和打印值

时间:2019-09-19 06:38:58

标签: flutter dart flutter-layout

我有一个不知道如何解决的问题: 在我的应用程序中,有一个Map从Firebase数据库检索数据,到目前为止一切正常。 现在,我要遍历地图,以在可滚动页面的容器内打印键和值。

我编写了这段代码,它可以工作,但是此解决方案使容器可滚动而不是页面:(

 Widget build(BuildContext context) {
 return Scaffold(
        backgroundColor: new Color(0xFFF8F8FF),
        appBar: new AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0.0,
          iconTheme: new IconThemeData(color: Colors.blueAccent),
        ),
 body: new Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            slider,
            carDescrContent
          ],
        ));
 final carDescrContent = new Expanded(
        child: new Container(
            constraints: BoxConstraints.expand(),
            decoration: new BoxDecoration(
                color: Colors.white,
                shape: BoxShape.rectangle,
                borderRadius: new BorderRadius.circular(8.0),
                boxShadow: <BoxShadow>[
                  new BoxShadow(
                      color: Colors.black12,
                      blurRadius: 5.0,
                      offset: new Offset(0.0, 6.0))
                ]),
            margin:
                const EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
            child: new ListView.builder(
                itemCount: map.length,
                itemBuilder: (BuildContext context, int index) {
                  String key = map.keys.elementAt(index);

                  return new Container(
                      margin: new EdgeInsets.symmetric(horizontal: 16.0),
                      child:
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[new Text("${key}"), new Text(map[key])],
                  ));
                })));

Screenshot of the page

谢谢

1 个答案:

答案 0 :(得分:0)

您应将carDescrContent的值包装在SingleChildScrollView中。就像一个盒子,它可以使孩子滚动。