如何从数组创建此构造?

时间:2020-03-23 13:55:20

标签: flutter dart

我有json格式的标记列表:

[ {“名称”:“ marker1”, “ dialog_text”:“ text1”, “ dialog_description”:“ text1”, “数”:50 } , {“ name”:“ marker2”, “ dialog_text”:“ text2”, “ dialog_description”:“ text2”, “数”:60 } , {“ name”:“ marker3”, “ dialog_text”:“ text3”, “ dialog_description”:“ text3”, “数”:70 } ... ]

我需要使用Dart从此JSON创建标记列表,如下所示:

     @override
      Widget build(BuildContext context) {
        var markers = <Marker>[
          Marker(
              width: 30.0,
              height: 30.0,
              point: london,
              builder: (ctx) => Container(
                    child: GestureDetector(
                      onTap: () {
                        showDialog(
                            context: context,
                            child: AlertDialog(
                              title: Text('My Super title'),
                              content: Text('Hello World'),
                            ));
                      },
                      child: ClipOval(
                        child: Container(
                          color: Colors.blue,
                          height: 30.0, // height of the button
                          width: 30.0, // width of the button
                          child: Center(child: Text('50')),
                        ),
                      ),
                    ),
                  )),

          Marker(
            width: 30.0,
            height: 30.0,
            point: dublin,
            builder: (ctx) => Container(
                child: GestureDetector(
                    onTap: () {
                      showDialog(
                          context: context,
                          child: AlertDialog(
                            title: Text('My Super title'),
                            content: Text('Hello World'),
                          ));
                    },
                    child: Container(
                      width: 30,
                      height: 30,
                      child: Icon(
                        Icons.favorite,
                        size: 20,
                      ),
                      decoration: BoxDecoration(
                          shape: BoxShape.circle, color: Color(0xFFe0f2f1)),
                    ))),
          ),

...
            ),
          )
        ];

我该怎么做?

我知道如何将json转换为数组,但是我不知道下一步该怎么做。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

    var objectArrays = /* your json array */
    var markers = objectArrays.map((object) {
      return Marker(
        width: 30.0,
        height: 30.0,
        point: london,
        builder: (ctx) => Container(
          child: GestureDetector(
            onTap: () {
              showDialog(
                  context: context,
                  child: AlertDialog(
                    title: Text(object.dialog_text),
                    content: Text(object.dialog_description),
                  ));
            },
            child: ClipOval(
              child: Container(
                color: Colors.blue,
                height: 30.0, // height of the button
                width: 30.0, // width of the button
                child: Center(child: Text('${object.count}')),
              ),
            ),
          ),
        ),
      );
    });
相关问题