简体   繁体   English

如何向页面视图 Flutter 添加不同的项目

[英]How to add different items to a Page View Flutter

I have this code which allows me to scroll between cards that hold text.我有这个代码,它允许我在包含文本的卡片之间滚动。 However, all it does is add the same text on each card.但是,它所做的只是在每张卡片上添加相同的文本。 I want to display two separate pieces of text which are kept inside我想显示保存在里面的两段单独的文本

HomeInfo()
Achieve()

This is the code I currently have:这是我目前拥有的代码:


SizedBox(
                    height: 300,
                      child: PageView.builder(
                        itemCount: 2,
                        controller: PageController(viewportFraction: 0.7),
                        onPageChanged: (int index) =>
                            setState(() => _index = index),
                        itemBuilder: (_, i) {
                          return Transform.scale(
                            scale: i == _index ? 1 : 0.9,
                            child: Card(
                              elevation: 6,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20)),
                              child: Center(
                                child: HomeInfo(),
                              ),
                            ),
                          );
                        },
                      ),
                    ), 

I am really confused on how to do this.我真的很困惑如何做到这一点。 (I just started getting into coding) Thanks (我刚开始接触编码)谢谢

You need to check for the index and return the appropriate widget.您需要检查索引并返回适当的小部件。

You can achieve this way:您可以通过这种方式实现:

SizedBox(
                    height: 300,
                      child: PageView.builder(
                        itemCount: 2,
                        controller: PageController(viewportFraction: 0.7),
                        onPageChanged: (int index) =>
                            setState(() => _index = index),
                        itemBuilder: (_, i) {

                          return Transform.scale(
                            scale: i == _index ? 1 : 0.9,
                            child: Card(
                              elevation: 6,
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20)),
                              child: Center(
                                child: i == 0 ? HomeInfo() : Achieve(), // this line
                              ),
                            ),
                          );
                        },
                      ),
                    ),  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM