当前位置: 首页 > 面试题库 >

如何在Flutter中创建Expandable ListView

顾俊哲
2023-03-14
问题内容

如何使用Flutter制作一个Expandable ListView,如下面的屏幕截图所示?

我想制作一个可滚动的列表视图,ExpansionTiles展开时会显示一个不可滚动的列表视图。

我尝试实现ExpansionTiles内部的列表视图,并使用嵌套了另一个列表视图listView.builder(...)。但是,当我展开
ExpansionTile列表视图时,它没有出现……

有没有办法在Flutter中获得类似的输出?

编辑:我的源代码:

import 'package:flutter/material.dart';

void main() => runApp(
  new MaterialApp(
    home: new MyApp(),
  )
);

var data = {
  "01/01/2018": [
    ["CocaCola", "\$ 5"],
    ["Dominos Pizza", "\$ 50"],
  ],

  "04/01/2018": [
    ["Appy Fizz", "\$ 10"],
    ["Galaxy S9+", "\$ 700"],
    ["Apple iPhone X", "\$ 999"],
  ],
};

List<String> dataKeys = data.keys.toList();

String getFullDate(String date) {
  List<String> dateSplit = date.split('/');
  List<String> months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"];

  return "${dateSplit[0]} ${months[int.parse(dateSplit[1]) - 1]} ${dateSplit[2]}";
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<Widget> _buildList(int keyIndex) {
    List<Widget> list = [];

    for (int i = 0; i < data[dataKeys[keyIndex]].length; i++) {
      list.add(
        new Row(
          children: <Widget>[
            new CircleAvatar(
              child: new Icon(Icons.verified_user),
              radius: 20.0,
            ),
            new Text(data[dataKeys[keyIndex]][i][0])
          ],
        )
      );
    }

    return list;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Expense Monitor"),
      ),
      body: new Container (
        child: new ListView.builder(
          itemCount: dataKeys.length,
          itemBuilder: (BuildContext context, int keyIndex) {
            return new Card(
              child: new ExpansionTile(
                title: new Text(getFullDate(dataKeys[keyIndex])),
                children: <Widget>[
                  new Column(
                    children: _buildList(keyIndex)
                  )
                ]
              ),
            );
          }
        )
      )
    );
  }
}

Error as shown in Console:

I/flutter (12945): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (12945): The following assertion was thrown during performResize():
I/flutter (12945): Vertical viewport was given unbounded height.
I/flutter (12945): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter (12945): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter (12945): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter (12945): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter (12945): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter (12945): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter (12945): the height of the viewport to the sum of the heights of its children.
I/flutter (12945): When the exception was thrown, this was the stack:
I/flutter (12945): #0      RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:944:15)
I/flutter (12945): #1      RenderViewport.performResize (package:flutter/src/rendering/viewport.dart:997:6)
I/flutter (12945): #2      RenderObject.layout (package:flutter/src/rendering/object.dart:1555:9)
I/flutter (12945): #3      _RenderProxyBox&RenderBox&RenderObjectWithChildMixin&RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:109:13)
......
I/flutter (12945): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (12945): Another exception was thrown: RenderBox was not laid out: RenderViewport#df29c NEEDS-LAYOUT NEEDS-PAINT

问题答案:

Try this:

import 'package:flutter/material.dart';

void main() => runApp(new MaterialApp(home: new MyApp(), debugShowCheckedModeBanner: false,),);

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView.builder(
        itemCount: vehicles.length,
        itemBuilder: (context, i) {
          return new ExpansionTile(
            title: new Text(vehicles[i].title, style: new TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold, fontStyle: FontStyle.italic),),
            children: <Widget>[
              new Column(
                children: _buildExpandableContent(vehicles[i]),
              ),
            ],
          );
        },
      ),
    );
  }

  _buildExpandableContent(Vehicle vehicle) {
    List<Widget> columnContent = [];

    for (String content in vehicle.contents)
      columnContent.add(
        new ListTile(
          title: new Text(content, style: new TextStyle(fontSize: 18.0),),
          leading: new Icon(vehicle.icon),
        ),
      );

    return columnContent;
  }
}

class Vehicle {
  final String title;
  List<String> contents = [];
  final IconData icon;

  Vehicle(this.title, this.contents, this.icon);
}

List<Vehicle> vehicles = [
  new Vehicle(
    'Bike',
    ['Vehicle no. 1', 'Vehicle no. 2', 'Vehicle no. 7', 'Vehicle no. 10'],
    Icons.motorcycle,
  ),
  new Vehicle(
    'Cars',
    ['Vehicle no. 3', 'Vehicle no. 4', 'Vehicle no. 6'],
    Icons.directions_car,
  ),
];


 类似资料:
  • 我能在flutter中创建一些类似于吐司的东西吗? 只是一个微小的通知窗口,不直接面对用户,不锁定或淡化后面的视图。

  • 我无法找到一种方法来创建一个输入字段在Flutter将打开一个下拉的名字列表。Flutter material Widgets可能吗? 就像这样

  • 如何创建类似于< code > floating action button 的东西?

  • 问题内容: 我一直在四处搜寻有关如何使用Swift 2.0为MapView制作MKCircle注释的良好解释,但我似乎找不到足够的解释。有人可以张贴一些示例代码来显示如何创建MKCircle批注吗?这是我用来制作地图并获取坐标的代码。 问题答案: 将展示有关如何使用xcode 8.3.3的swift 3在地图视图上创建圆形叠加层的分步方法 在您的主故事板文件中,将地图工具包视图拖到故事板的场景(视

  • 本文向大家介绍如何在C#中创建StringBuilder?,包括了如何在C#中创建StringBuilder?的使用技巧和注意事项,需要的朋友参考一下 要在C#中创建StringBuilder,代码如下- 示例 输出结果 这将产生以下输出- 示例 让我们看另一个例子- 输出结果 这将产生以下输出-

  • 本文向大家介绍如何在JavaFX中创建RadioMenuItem?,包括了如何在JavaFX中创建RadioMenuItem?的使用技巧和注意事项,需要的朋友参考一下 菜单是提供给用户的选项或命令的列表,通常菜单包含执行某些操作的项目。菜单的内容称为菜单项,菜单栏包含多个菜单。 JavaFx支持三种菜单项,即-检查菜单项,自定义菜单项和单选菜单项。 RadioMenuItem RadioMenuI