2

我已经创建了一个像这样的简单列表视图

ListView DiagnosisListWidget(BuildContext context) {
  return ListView.builder(
    shrinkWrap: true,
    scrollDirection: Axis.horizontal,
    itemCount: global_Diagnosis.length,
    itemBuilder: (BuildContext context, int index) => Padding(
      padding: const EdgeInsets.only(left: 13),
      child: Container(
        width: 300,
        height: 100,
        child: GestureDetector(
          // onTap: widget.onPressed,
          onTap: () {},
          child: RichText(
                      text: new TextSpan(
                        // Note: Styles for TextSpans must be explicitly defined.
                        // Child text spans will inherit styles from parent
                        style: new TextStyle(
                          fontSize: 14.0,
                          color: Colors.black,
                        ),
                        children: <TextSpan>[
                          new TextSpan(
                              text: 'Name : ',
                              style: new TextStyle(
                                  fontSize: 13, fontFamily: 'PoppinsMedium')),
                          new TextSpan(
                              text: '${global_Diagnosis[index]['diagName']}',
                              style: TextStyle(
                                  fontSize: 13,
                                  fontFamily: 'PoppinsRegular',
                                  color: textGreyColor)),
                        ],
                      ),
                    )),
      ),
    ),
  );
}

我只是在不同的页面上调用它,我面临的问题是它没有更新意味着我在哪个页面上添加 global_Diagnosis 并显示这个 listView 我需要返回并再次查看我的列表或需要使用 setState。我想要一些简单的解决方案,它会在没有 setState 的情况下自动更改它

我尝试的是创建一个这样的类

 var global_Diagnosis = [];

class DiagnosisItemClass extends ChangeNotifier {
  DiagnosisItemClass(String name, int id, String comments) {
    _name = name;
    _comments = comments;
    _id = id;
  }
  String _comments; // to know active index
  String _name;
  int _id;

  void toggleSelected() {
    notifyListeners(); // To rebuild the Widget
  }

  String getName() {
        notifyListeners(); // To rebuild the Widget

    return _name;
  }

  String getComments() {
        notifyListeners(); // To rebuild the Widget

    return _comments;
  }

  int getID() {
        notifyListeners(); // To rebuild the Widget

    return _id;
  }
}

我正在像这样添加我的数组

global_Diagnosis.add(DiagnosisItemClass(_quickList[index]['diagnosisName'], _quickList[index]['diagnosisId'], _comments.text));

并像这样显示

  ListView DiagnosisListWidget(BuildContext context) {

    return ListView.builder(
      shrinkWrap: true,
      scrollDirection: Axis.horizontal,
      itemCount: global_Diagnosis.length,
      itemBuilder: (BuildContext context, int index) => Padding(
        padding: const EdgeInsets.only(left: 13),
        child: Container(
          width: 300,
          height: 100,
          child: GestureDetector(
            // onTap: widget.onPressed,
            onTap: () {},
            child: RichText(
                        text: new TextSpan(
                          // Note: Styles for TextSpans must be explicitly defined.
                          // Child text spans will inherit styles from parent
                          style: new TextStyle(
                            fontSize: 14.0,
                            color: Colors.black,
                          ),
                          children: <TextSpan>[
                            new TextSpan(
                                text: 'Name : ',
                                style: new TextStyle(
                                    fontSize: 13,
                                    fontFamily: 'PoppinsMedium')),
                            new TextSpan(
                                text: '${global_Diagnosis[index].getName()}',
                                style: TextStyle(
                                    fontSize: 13,
                                    fontFamily: 'PoppinsRegular', color: textGreyColor)),
                          ],
                        ),
                      ),
                      ),
        ),
      ),
    ); 
  }

我不知道我是否在尝试正确的方向,但我已经阅读了一些答案,有人在做这样的事情,但它对我不起作用。

4

0 回答 0