Flutter setState()正在执行,但窗口小部件无法重新渲染

时间:2020-07-20 16:12:58

标签: flutter flutter-web

我正在建立基于Flutter的网页,因为我使用了GMaps并提供了标记,我在地图的右侧创建了一个用户列表,以便每当用户单击图块时,该用户就会用该用户标记位置应该在地图上显示,其余位置应该不可见,因此我在userTile onTap属性中使用了setState(),但是setState正在执行,但是Map StateFull小部件没有得到重建。

Plz,救救我..... 在此先感谢。

class Member extends StatelessWidget {
  const Member({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final DatabaseManager _dbRef = DatabaseManager.getDbReference;
    Stream<List<MemberData>> memberData = _dbRef.getMembers();
    return StreamProvider<List<MemberData>>(
      create: (_) => memberData,
      child: MembersPage(),
    );
  }
}

class MembersPage extends StatefulWidget {
  const MembersPage({Key key}) : super(key: key);

  @override
  _MembersPageState createState() => _MembersPageState();
}

class _MembersPageState extends State<MembersPage> {
  final Constants _constants = Constants.getReferenceObject;
  bool isExpanded = false;
  double expandWidth = 0;
    bool setMarkerCall = false;
    Marker data;

  String setInfoData(String title, String head, String body) {
    return '<div id="content' +
        title +
        '">' +
        '<div id="bodyContent">' +
        '<p>' +
        '<h3>' +
        title +
        '</h3>' +
        '<b>' +
        head +
        '</b><br>' +
        body +
        '</p>' +
        '</div>';
  }

  // void callback

  @override
  Widget build(BuildContext context) {
    var memberData = Provider.of<List<MemberData>>(context);
    if (memberData != null) {
      return Stack(
        children: [
          Row(
            children: [
              AnimatedContainer(
                duration: Duration(milliseconds: 300),
                constraints: BoxConstraints(minHeight: double.maxFinite),
                width: expandWidth,
                color: Colors.grey[100],
                child: Align(
                  alignment: Alignment.topCenter,
                  child: Visibility(
                    visible: isExpanded,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: Text(
                            'MEMBER\'S',
                            style: TextStyle(
                              fontFamily: 'ProductSans',
                              fontSize: 20.0,
                            ),
                          ),
                        ),
                        Container(
                          width: 500.0,
                          child: ListView.builder(
                            shrinkWrap: true,
                            itemBuilder: (context, index) => Padding(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 8.0,
                                vertical: 5.0,
                              ),
                              child: InkWell(
                                onTap: () {
                                  setState(() {
                                    data = Marker(
                                      GeoCoord(
                                          double.parse(
                                              memberData[index].latitude),
                                          double.parse(
                                              memberData[index].longitude)),
                                      info: setInfoData(
                                          memberData[index].shopName,
                                          memberData[index].memberName,
                                          memberData[index].shopAddress),
                                      infoSnippet: ' ',
                                      label: 'X',
                                      onInfoWindowTap: () {},
                                    );
                                    setMarkerCall = !setMarkerCall;
                                    print(setMarkerCall);
                                  });
                                },
                                child: Material(
                                  elevation: 5.0,
                                  borderRadius: BorderRadius.circular(8.0),
                                  child: Padding(
                                    padding: const EdgeInsets.symmetric(
                                      vertical: 14.0,
                                    ),
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: [
                                        dataCell(30.0, (index + 1).toString()),
                                        dataCell(150.0,
                                            memberData[index].memberName),
                                        dataCell(
                                            200.0, memberData[index].shopName),
                                        dataCell(
                                            100.0, memberData[index].contact),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            itemCount: memberData.length,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              Expanded(
                child: Container(
                  height: _constants.height,
                  width: _constants.width,
                  child: Map(
                    data: data,
                    setMarkerCall: setMarkerCall,
                  ),
                ),
              ),
            ],
          ),
          Container(
            child: Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                AnimatedContainer(
                  duration: Duration(milliseconds: 300),
                  width: expandWidth,
                  height: 1.0,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(
                      vertical: 12.0, horizontal: 12.0),
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        isExpanded ? expandWidth = 0.0 : expandWidth = 500.0;
                        isExpanded = !isExpanded;
                      });
                    },
                    child: Material(
                      borderRadius: BorderRadius.circular(30.0),
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                            vertical: 8.0, horizontal: 12.0),
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Icon(
                              Ionicons.md_people,
                              color: Colors.black,
                              size: 24.0,
                            ),
                            SizedBox(
                              width: 5.0,
                            ),
                            Text(
                              'Member\'s',
                              style: TextStyle(
                                fontFamily: 'ProductSans',
                                fontSize: 20.0,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      );
    } else {
      return Container(
        height: 0,
        width: 0,
      );
    }
  }
Widget dataCell(double width, String string) {
    return Container(
      width: width,
      child: Center(
        child: Text(
          string,
          overflow: TextOverflow.fade,
        ),
      ),
    );
  }
}

这是我的代码...

我的问题是与此setState调用有关:

() {
                                  setState(() {
                                    data = Marker(
                                      GeoCoord(
                                          double.parse(
                                              memberData[index].latitude),
                                          double.parse(
                                              memberData[index].longitude)),
                                      info: setInfoData(
                                          memberData[index].shopName,
                                          memberData[index].memberName,
                                          memberData[index].shopAddress),
                                      infoSnippet: ' ',
                                      label: 'X',
                                      onInfoWindowTap: () {},
                                    );
                                    setMarkerCall = !setMarkerCall;
                                    print(setMarkerCall);
                                  });
                                },

我还有一个setState调用,用于userList扩展及其工作。.

1 个答案:

答案 0 :(得分:0)

您已在 build()方法中声明了 setMarkerCall 数据。因此,每次调用 setStates()时,框架都会调用 build()方法,并重新初始化 setMarkerCall 数据因此,您不会注意到该变量状态的任何变化。

@override
  Widget build(BuildContext context) {
    bool setMarkerCall = false;
    Marker data;
    var memberData = Provider.of<List<MemberData>>(context);
    if (memberData != null) {
      return Stack(
        children: [

只需在 build()方法之外声明变量。

bool setMarkerCall = false;
Marker data;

@override
  Widget build(BuildContext context) {
    var memberData = Provider.of<List<MemberData>>(context);
    if (memberData != null) {
      return Stack(
        children: [