ListView.builder中的”chain text”应翻译为”链式文本”。

huangapple go评论56阅读模式
英文:

chain text from ListView.builder in flutter?

问题

以下是您要翻译的代码部分:

class TopNewsBar extends StatelessWidget {
  const TopNewsBar({super.key});

  @override
  Widget build(BuildContext context) {
    var constraints = MediaQuery.of(context).size.width;
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: constraints * .03),
      child: Container(
        decoration:
            BoxDecoration(color: grey, borderRadius: BorderRadius.circular(14)),
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 14.0, horizontal: 25),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  'Trending Now: ',
                  style: h3b.copyWith(color: red),
                ),
              ),
              Flexible(
                flex: 11,
                child: StreamBuilder(
                  stream: FirebaseFirestore.instance
                      .collection("Posts")
                      .orderBy("timestamp", descending: true)
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return ListView.builder(
                          itemCount: 5,
                          physics: const NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (context, index) {
                            final post = snapshot.data!.docs[index];
                            final scrollingtext = post["title"] + "    •    ";
                            final fulltext = scrollingtext;
                            return TextScroll(fulltext);
                          });
                    } else if (snapshot.hasError) {
                      return Center(
                        child: Text('Error${snapshot.error}'),
                      );
                    } else {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

希望这能帮助您。如果您有任何其他问题,请随时提出。

英文:

is there a way of chaining (joining?) text together from ListView.builder so it is just one long text? I've tried getting this done with GridView instead but the aspect ratio messes the whole thing up. I'm using TextScroll (https://pub.dev/packages/text_scroll), which works great but I just can't figure out how to connect the text together. the option to join() isn't there?

What I'm after:ListView.builder中的”chain text”应翻译为”链式文本”。

And what I'm getting ListView.builder中的”chain text”应翻译为”链式文本”。 ListView.builder中的”chain text”应翻译为”链式文本”。

My code:

    class TopNewsBar extends StatelessWidget {
  const TopNewsBar({super.key});

  @override
  Widget build(BuildContext context) {
    var constraints = MediaQuery.of(context).size.width;
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: constraints * .03),
      child: Container(
        decoration:
            BoxDecoration(color: grey, borderRadius: BorderRadius.circular(14)),
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 14.0, horizontal: 25),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: 1,
                child: Text(
                  'Trending Now: ',
                  style: h3b.copyWith(color: red),
                ),
              ),
              Flexible(
                flex: 11,
                child: StreamBuilder(
                  stream: FirebaseFirestore.instance
                      .collection("Posts")
                      .orderBy("timestamp", descending: true)
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return ListView.builder(
                          itemCount: 5,
                          physics: const NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (context, index) {
                            final post = snapshot.data!.docs[index];
                            final scrollingtext = post["title"] + "    •    ";
                            final fulltext = scrollingtext;
                            return TextScroll(fulltext);
                          });
                    } else if (snapshot.hasError) {
                      return Center(
                        child: Text('Error${snapshot.error}'),
                      );
                    } else {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

答案1

得分: 1

你可以简单地将文本连接在一起,然后使用一个 TextScroll 来显示,而不是使用 ListView

builder: (context, snapshot) {
  if (snapshot.hasData) {
    final posts = snapshot.data!.docs;
    final joinedPostTitle = posts.map((post) => post['title'] as String).join('    •    ');
    return TextScroll(joinedPostTitle);
  } else if (snapshot.hasError) {
    return Center(
      child: Text('Error${snapshot.error}'),
    );
  } else {
    return const Center(
      child: CircularProgressIndicator(),
    );
  }
}
英文:

You can simply join the texts together and display it using one TextScroll
instead of using a ListView.

builder: (context, snapshot) {
  if (snapshot.hasData) {
    final posts = snapshot.data!.docs;
    final joinedPostTitle = posts.map((post) => post['title'] as String).join('        ');
    return TextScroll(joinedPostTitle);
  } else if (snapshot.hasError) {
    return Center(
      child: Text('Error${snapshot.error}'),
    );
  } else {
    return const Center(
      child: CircularProgressIndicator(),
    );
  }
}

huangapple
  • 本文由 发表于 2023年7月6日 20:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定