简体   繁体   English

如何在使用 map() 时使用 listTile 在 Flutter 中制作分隔符?

[英]How to make a divider in Flutter using listTile while using map()?

I am creating a listView using a.map(), and I want to put a divider between the lines.我正在使用 a.map() 创建一个 listView,并且我想在两行之间放置一个分隔符。 I want to use either the Divider() method or listTiles.divideTiles(), but I don't know how to implement it well.我想使用 Divider() 方法或 listTiles.divideTiles(),但我不知道如何很好地实现它。 I don't know if this isn't possible or if I don't really understand the method(which it may be because I am fairly new to Flutter), but I think my app would look a lot better with the dividers.我不知道这是否不可能,或者我是否真的不了解该方法(这可能是因为我对 Flutter 还很陌生),但我认为我的应用程序使用分隔符会更好看。 Here is the build method I want to create them in:这是我要在其中创建它们的构建方法:

  Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(title: Text("Grocery Lists")),
  drawer: AppDrawer(),
  body: ListView(
    children: groceryListNames.map(
          (data) => ListTile(
            trailing: const Icon(Icons.arrow_forward_ios),
            title: Text(
              data,
              style: GoogleFonts.biryani(fontSize: 24),
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => GroceryListPage(
                          listTitle: "$data Grocery List",
                        )),
              );
            },
          ),
        )
        .toList(),
  ),
);

} }

While working with dynamically-generated List Views you should use ListView.separated() (similar to ListView.builder() ), which gives a better performance due to on-the-go rendering:在使用动态生成的列表视图时,您应该使用ListView.separated() (类似于ListView.builder() ),由于实时渲染,它可以提供更好的性能:

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text("Grocery Lists")),
    drawer: AppDrawer(),
    body: ListView.separated(
      itemCount: groceryListNames.length,
      separatorBuilder: (_, __) => const Divider(),
      builder: (context, index) {
          final data = groceryListNames[index];
          return ListTile(
              trailing: const Icon(Icons.arrow_forward_ios),
              title: Text(
                data,
                style: GoogleFonts.biryani(fontSize: 24),
              ),
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => GroceryListPage(
                            listTitle: "$data Grocery List",
                          )),
                );
              },
            ),
          ),},
    ),
  );
}

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

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