问题综合浏览量在setState(Flutter)之后重新加载第一页

时间:2019-12-06 10:10:51

标签: flutter pageviews stream-builder flutter-pageview

我有一个代码,此代码创建有关某些用户的网页浏览,数据来自firebase

return new Scaffold(
    appBar: new AppBar(
      title: new Text("Carousel"),
    ),
    body: StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection('users').snapshots(),
      builder:
          (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return new CircularProgressIndicator();
          default:
            return new PageView(
              onPageChanged: _onPageViewChange,
              controller: _controller,
              scrollDirection: Axis.horizontal,
              children:
              snapshot.data.documents.map((DocumentSnapshot document) {
                return new Column(
                  children: <Widget>[
                    new Container(
                      child: new ClipOval(
                          child: new CachedNetworkImage(
                            width: 150.0,
                            height: 150.0,
                            imageUrl: document['img'],
                            fit: BoxFit.fill,
                            placeholder: (context, url) =>
                                CircularProgressIndicator(),
                            errorWidget: (context, url, error) =>
                                Icon(Icons.error),
                          )),
                    ),
                    new ListTile(
                      title: new Text(
                          isPerson
                              ? 'My name is'
                              : (isPlace
                              ? 'My favourite is'
                              : (isNote
                              ? 'I am from'
                              : (isPhone
                              ? 'My phone is'
                              : (isLock ? '' : '')))),
                          textAlign: TextAlign.center),
                      subtitle: new Text(
                        isPerson
                            ? document['name']
                            : (isPlace
                            ? document['place']
                            : (isNote
                            ? document['note']
                            : (isPhone
                            ? document['phone']
                            : (isLock
                            ? document['lock'].toString()
                            : "")))),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    buildButton1(Icons.person)
                  ],
                );
              }).toList(),
            );
        }
      },
    ));
}

这是功能buildButton1()

Widget buildButton1(IconData icon) {
  return new Column(
    children: <Widget>[
      new Container(
        padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 20.0),
        child: new IconButton(
          icon: Icon(icon),
          onPressed: () {
            setState(() {
              //isChecked ? true : false;
              isPerson = true;
              isNote = false;
              isPlace = false;
              isPhone = false;
              isLock = false;
            });
          },
          iconSize: 32.0,
          color: isPerson ? Colors.green : Colors.grey,
        ),
      )
    ],
  );
}

当我按下按钮设置变量时,页面浏览器将重新加载并显示首页。我该如何解决这个问题。这是示例图片https://imgur.com/nKC358E ................................................... ..............................................

1 个答案:

答案 0 :(得分:0)

此问题来自_onPageViewChange函数。

最后一页没有返回整数值。如果您有3页,那么最后返回的索引将是1.99999999999 ...而不是2。

我解决了这样的问题

onPageChanged: (index){
    setState(() {
        if (index > 1.99){
            lastPage=true;
        }else{
            lastPage=false;
        }
    });
}