简体   繁体   English

Flutter:使用 ListView.builder() 和 ListTile 显示长部件列表

[英]Flutter : Showing long widget list using ListView.builder() and ListTile

I have a Widget list with millions of widgets and some of them are wider than screen.我有一个包含数百万个小部件的小部件列表,其中一些比屏幕还宽。

I need to use ListView.builder() and ListTile to save time while running like this:我需要使用 ListView.builder() 和 ListTile 来节省运行时间:

ListView.builder(
      scrollDirection: Axis.vertical,
      shrinkWrap: true,
      controller: scrollController,
      itemCount: widgetList.length,
      itemBuilder: (context, i) => 
         ListTile(
           title: widgetList[i]
           )
      );

But when I run it, those ListTile that their title is wider than screen will overflow on the right because ListView.builder() not wide enough.但是当我运行它时,那些标题比屏幕宽的 ListTile 会在右边溢出,因为 ListView.builder() 不够宽。

If I assign a big width to ListView.builder() like this will works fine but will remain a lot of blank even if all widgets in list are short:如果我像这样给 ListView.builder() 分配一个大宽度会工作正常,但即使列表中的所有小部件都很短,也会保留很多空白:

ListView(
      shrinkWrap: true,
      scrollDirection: Axis.horizontal,
      children: [
          Container(
             width: 2000,
             child: ListView.builder(
               scrollDirection: Axis.vertical,
               shrinkWrap: true,
               controller: scrollController,
               itemCount: widgetList.length,
               itemBuilder: (context, i) => 
                  ListTile(
                    title: widgetList[i]
                    )
               ))]);

Any idea to improve this function?有什么改进这个 function 的想法吗?

---------------update----------- - - - - - - - -更新 - - - - - -

My widgetList contains Column/Row etc.我的 widgetList 包含列/行等。

But let's start with only one Text().但是让我们从只有一个 Text() 开始。

Example:例子:

ListView.builder(
    restorationId: 'sampleItemListView',
    itemCount: 1000000,
    itemBuilder: (BuildContext context, int index) {
      return ListTile(
        title: Text(
          "abcdefghijklmnopqrstuvwxyz"
          "abcdefghijklmnopqrstuvwxyz"
          "abcdefghijklmnopqrstuvwxyz",
          maxLines: 1,
        ),
      );
    },
  )

The text is too long but I don't want it change line.文字太长,但我不想换行。

What can I do to read full text?我该怎么做才能阅读全文?

Use like below, do not specify the hight and width for the widgets像下面这样使用,不要为小部件指定高度和宽度

@override
  Widget build(BuildContext context) {
    var items = List<SampleItem>.generate(
        1000,
        (i) => const SampleItem(
              '范植勝',
              'Showing long widget list using ListView.builder() and ListTile',
              Icons.favorite,
            ));
    return Scaffold(
      body: ListView.builder(
        restorationId: 'sampleItemListView',
        itemCount: items.length,
        itemBuilder: (BuildContext context, int index) {
          final item = items[index];
          return ListTile(
              title: Text(item.name),
              subtitle: Text(item.decsription),
              leading: Icon(item.icons),
              onTap: () {
                Navigator.restorablePushNamed(
                  context,
                  SampleItemDetailsView.routeName,
                );
              });
        },
      ),
    );
  }

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

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