根据Flutter SQFlite的数据分配图标值

发布于 01-21 23:22 字数 5300 浏览 2 评论 0原文

我正在尝试根据未来构建器内部数据库的数据在卡上加载喜欢的按钮。在ListView.builder中使用以下代码时,将值设置为true,按钮点击不会更改它。我认为这是因为Futurebuilder。

txtData.zFavourite == null
                    ? isFavourite[index] = false
                    : isFavourite[index] = true;

它还需要初始化iSfavureite的值,如果它是在小部件之外分配的,则我不会从数据库中获取数据,因为它直接加载到了将来的构建器中。 我们如何根据数据库中的数据分配ISFAVURITE的值并根据按钮单击更改喜爱的图标?

我也检查了此链接到处理按钮单击单个列表。

late List<bool> isFavourite = [false, false];
Widget _displayZhesa() {
    return FutureBuilder<List<Zhebsa>>(
        future: _getZhebsa(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          if (snapshot.hasData) {
            return ListView.builder(
              itemCount: snapshot.data!.length,
              itemBuilder: (context, index) {
                final txtData = snapshot.data![index];
                txtData.zFavourite == null
                    ? isFavourite[index] = false
                    : isFavourite[index] = true;
                return Card(
                  elevation: 10,
                  color: Colors.white70,
                  shadowColor: Colors.amber[500],
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(5.0),
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: Colors.orange[400],
                          border: Border.all(
                            color: Colors.amber.shade100,
                            width: 2,
                          ),
                        ),
                        child: Center(
                          child: Text(
                            sQuery,
                            style: const TextStyle(
                                color: Colors.black,
                                fontSize: 20,
                                fontWeight: FontWeight.w500),
                          ),
                        ),
                      ),
                      Row(
                        children: <Widget>[
                          Center(
                            child: IconButton(
                              onPressed: () {
                                setState(() {
                                  isFavourite[index] = !isFavourite[index];
                                });
                              },
                              /* () =>
                                  _setFaviurite(txtData.zFavourite, index), */
                              icon: Icon(
                                isFavourite[index]
                                    ? Icons.favorite_sharp
                                    : Icons.favorite_border_sharp,
                                size: 30.0,
                                color: Colors.redAccent,
                              ),
                            ),
                          ),
                          Expanded(
                            child: ListTile(
                              title: const Text('ཞེ་ས།'),
                              subtitle: Text(txtData.zWord), //ཞེ་སའི་ཚིག
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(right: 20),
                            child: IconButton(
                              onPressed: () {
                                isPlayingPronunciation
                                    ? stopPronunciation()
                                    : _playPronunciation(
                                        '${txtData.zPronunciation}');
                                setState(() {
                                  isPlayingPronunciation =
                                      !isPlayingPronunciation;
                                });
                              },
                              icon: Icon(
                                isPlayingPronunciation
                                    ? Icons.stop_circle_outlined
                                    : Icons.volume_up,
                                size: 50.0,
                                color: Colors.blue,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 30.0),
                        child: ListTile(
                          title: const Text('དཔེར་བརྗོད།'),
                          subtitle: SelectableText('${txtData.zPhrase}'),
                        ),
                      ),
                    ],
                  ),
                );
              },
            );
          } else {
            return const Text('No Data');
          }
        });
  }

I'm trying to load the favorite button on my card based on the data from database inside the future builder. while using the following code inside ListView.Builder, is sets the value to true and button click does not change it. I think it is because of FutureBuilder.

txtData.zFavourite == null
                    ? isFavourite[index] = false
                    : isFavourite[index] = true;

it also requires to initialize the value for isFavourite and if it is assigned outside the widget, I do not get the data from database as it directly loads into the future builder.
how do we assign the value for isFavourite based on the data from the database and change the favourite icon based on the button click?

I also checked this link to handle button clicks for the individual list.

late List<bool> isFavourite = [false, false];
Widget _displayZhesa() {
    return FutureBuilder<List<Zhebsa>>(
        future: _getZhebsa(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          if (snapshot.hasData) {
            return ListView.builder(
              itemCount: snapshot.data!.length,
              itemBuilder: (context, index) {
                final txtData = snapshot.data![index];
                txtData.zFavourite == null
                    ? isFavourite[index] = false
                    : isFavourite[index] = true;
                return Card(
                  elevation: 10,
                  color: Colors.white70,
                  shadowColor: Colors.amber[500],
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: const EdgeInsets.all(5.0),
                        width: double.infinity,
                        decoration: BoxDecoration(
                          color: Colors.orange[400],
                          border: Border.all(
                            color: Colors.amber.shade100,
                            width: 2,
                          ),
                        ),
                        child: Center(
                          child: Text(
                            sQuery,
                            style: const TextStyle(
                                color: Colors.black,
                                fontSize: 20,
                                fontWeight: FontWeight.w500),
                          ),
                        ),
                      ),
                      Row(
                        children: <Widget>[
                          Center(
                            child: IconButton(
                              onPressed: () {
                                setState(() {
                                  isFavourite[index] = !isFavourite[index];
                                });
                              },
                              /* () =>
                                  _setFaviurite(txtData.zFavourite, index), */
                              icon: Icon(
                                isFavourite[index]
                                    ? Icons.favorite_sharp
                                    : Icons.favorite_border_sharp,
                                size: 30.0,
                                color: Colors.redAccent,
                              ),
                            ),
                          ),
                          Expanded(
                            child: ListTile(
                              title: const Text('ཞེ་ས།'),
                              subtitle: Text(txtData.zWord), //ཞེ་སའི་ཚིག
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(right: 20),
                            child: IconButton(
                              onPressed: () {
                                isPlayingPronunciation
                                    ? stopPronunciation()
                                    : _playPronunciation(
                                        '${txtData.zPronunciation}');
                                setState(() {
                                  isPlayingPronunciation =
                                      !isPlayingPronunciation;
                                });
                              },
                              icon: Icon(
                                isPlayingPronunciation
                                    ? Icons.stop_circle_outlined
                                    : Icons.volume_up,
                                size: 50.0,
                                color: Colors.blue,
                              ),
                            ),
                          ),
                        ],
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 30.0),
                        child: ListTile(
                          title: const Text('དཔེར་བརྗོད།'),
                          subtitle: SelectableText('${txtData.zPhrase}'),
                        ),
                      ),
                    ],
                  ),
                );
              },
            );
          } else {
            return const Text('No Data');
          }
        });
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文