2

我有一个代码,这段代码创建了一个关于某些用户的综合浏览量,数据来自 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,
        ),
      )
    ],
  );
}

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

4

1 回答 1

1

问题来自 _onPageViewChange 函数。

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

我解决了这样的问题

onPageChanged: (index){
    setState(() {
        if (index > 1.99){
            lastPage=true;
        }else{
            lastPage=false;
        }
    });
}
于 2020-05-04T03:26:43.080 回答