简体   繁体   English

(Flutter) DataTable 行的 DataCell 内的水平 ListView.Builder

[英](Flutter) Horizontal ListView.Builder inside a DataCell of a DataTable row

I should say that I always have issues with getting ListViews inside columns, rows, etc. without getting tons of errors and having to really find workarounds.我应该说我总是在获取列、行等中的 ListViews 时遇到问题,而不会出现大量错误并且必须真正找到解决方法。 I've searched for this one and cannot find a solution that works.我已经搜索了这个,但找不到有效的解决方案。

I'm trying to put a ListView.builder inside each DataCell of a DataTable.我正在尝试将 ListView.builder 放在 DataTable 的每个 DataCell 中。 Here is an example I made up.这是我编的一个例子。

I have tried so many different things - putting the ListView in a Container with set width / height, putting it in Flexible, putting it in a Column / Row, and on and on and on.我尝试了很多不同的东西 - 将 ListView 放在一个具有设置宽度/高度的容器中,将它放在灵活中,将它放在一个列/行中,等等等等。 I've also tried all kinds of variations of the properties within the ListView.builder (ie shrinkWrap on and off, physics and no physics, etc.), and each one gives me new errors and none of them work.我还尝试了 ListView.builder 中属性的各种变体(即shrinkWrap 开和关、物理和无物理等),每一个都给了我新的错误,但没有一个起作用。

  Widget build(BuildContext context) {
List<DataRow> _testRows = [
  DataRow(
    cells: [
      DataCell(
        ListView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemCount: 3,
          scrollDirection: Axis.horizontal,
          itemBuilder: (BuildContext context, int index) {
            return Text(index.toString());
          },
        ),
      ),
      DataCell(
        Text('Data B'),
      ),
      DataCell(
        Text('Data C'),
      ),
    ],
  ),
];

return Scaffold(
  body: DataTable(
    columns: const <DataColumn>[
      DataColumn(
        label: Text('Column A'),
      ),
      DataColumn(
        label: Text('Column B'),
      ),
      DataColumn(
        label: Text('Column C'),
      ),
    ],
    rows: _testRows,
  ),
);

} }

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
In this case, you can use function _testRows() and wrap ListView with Container and set width在这种情况下,您可以使用function _testRows()并用Container包装ListView并设置width
code snippet代码片段

 List<DataRow> _testRows() {
    return [
      DataRow(
        cells: [
          DataCell(
            Container(
              width: 100,
              child: ListView.builder(

working demo工作演示

在此处输入图片说明

full code完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<DataRow> _testRows() {
    return [
      DataRow(
        cells: [
          DataCell(
            Container(
              width: 100,
              child: ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: 3,
                scrollDirection: Axis.horizontal,
                itemBuilder: (BuildContext context, int index) {
                  return Text(index.toString());
                },
              ),
            ),
          ),
          DataCell(
            Text('Data B'),
          ),
          DataCell(
            Text('Data C'),
          ),
        ],
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: DataTable(columns: const <DataColumn>[
          DataColumn(
            label: Text('Column A'),
          ),
          DataColumn(
            label: Text('Column B'),
          ),
          DataColumn(
            label: Text('Column C'),
          ),
        ], rows: _testRows()),
      ),
    );
  }
}

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

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