Flutter A RenderFlex 溢出 X 像素右侧

时间:2021-01-25 12:55:04

标签: flutter dart

我正在通过创建我们的 wpf 应用程序之一来学习 flutter 和 dart。

但是,我的 DataTable 抛出异常:

======== Exception caught by rendering library =====================================================
       A RenderFlex overflowed by 10.0 pixels on the right.

做了一些研究,可以选择使用 ExpandedSingleChildScrollView 等,但仍然没有任何帮助。

如果我将数据行字符串变小,一切都会好起来。

小部件代码

class WorkList extends StatefulWidget {
   WorkListState createState() => new WorkListState();
}

class WorkListState extends State<WorkList> with SingleTickerProviderStateMixin {
   List<DataRow> _activities = [
     DataRow(
       cells: <DataCell>[
          DataCell(Text('William')),
          DataCell(Text('27')),
          DataCell(Text('Associate Professor')),
          DataCell(Text('Associate Professor1')),
          DataCell(Text('Associate Professor1')),
    ]
  )
];

void initState() {
  super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]);
  _controller = TabController(length: 2, vsync: this);
  _controller.addListener(() {
    _filterActivities();
  });
}
@override
dispose(){
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  super.dispose();
}

TabBar get _tabBar => TabBar(
  labelColor: Colors.white,
  controller: _controller,
  indicator: BoxDecoration(
    color: orangeBackground,
  ),
  tabs: [
    Tab(text: 'SHOW CURRENT'),
    Tab(text: 'SHOW ALL')
  ],
);

TabController _controller;

DataTable get _dataTable => DataTable(
  columns: const <DataColumn>[
    DataColumn(
      label: Text(
        'First Name',
        style: TextStyle(fontStyle: FontStyle.italic),
      ),
    ),
    DataColumn(
      label: Text(
        'Last Name',
        style: TextStyle(fontStyle: FontStyle.italic),
      ),
    ),
    DataColumn(
      label: Text(
        'Id',
        style: TextStyle(fontStyle: FontStyle.italic),
      ), 
    ),
    DataColumn(
      label: Text(
        'Time',
        style: TextStyle(fontStyle: FontStyle.italic),
      ),
    ),
    DataColumn(
      label: Text(
        'Operator',
        style: TextStyle(fontStyle: FontStyle.italic),
      ),
    ),
  ],
  rows: _activities,
);

void _filterActivities() {
  setState(() {
    _dataTable.rows.clear();
    _activities.add(
      DataRow(
          cells: <DataCell>[
            DataCell(Text('Some random')),
            DataCell(Text('Some random1')),
            DataCell(Text('Associate Professor1111111111dasdasds')),
            DataCell(Text('Associate Professor1111111adssaa')),
            DataCell(Text('TEEEEEEEEEEESTdsdasds')),
          ]
      )
    );
  });
}

@override
Widget build(BuildContext context) {
  return DefaultTabController(
      length: 2,
      child: Scaffold(
        appBar: AppBar(
          bottom: PreferredSize(
            preferredSize: _tabBar.preferredSize,
            child: ColoredBox(
              color: midDarkBackground,
              child: _tabBar,
            ),
          ),
          title: Text('Worklist'),
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: TabBarView(
                controller: _controller,
                physics: NeverScrollableScrollPhysics(), // disable swipe
                children: [
                  _dataTable,
                  _dataTable
                ],
              ),
            )
          ],
        ),
        floatingActionButton: FloatingActionButton.extended(
          label: Text('CREATE NEW PROCEDURE'),
          backgroundColor: darkBackground,
          foregroundColor: Colors.white,
      ),
    ),
  );
}
}

1 个答案:

答案 0 :(得分:3)

如果您想为您的表格水平滚动,只需用 DataTableSingleChildScrollView 包裹 scrollDirection: Axis.horizontal 小部件。

因此您的 get _dataTable 将如下所示:

get _dataTable => SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: DataTable(
    columns: const <DataColumn>[...],
    rows: ...,
  ),
);

结果:

horizontal scroll

如果你想有一个双向滚动,像这样用第二个 SingleChildScrollView 包裹它:

get _dataTable => SingleChildScrollView(
  scrollDirection: Axis.vertical,
  child: SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: DataTable(
      columns: const <DataColumn>[...],
      rows: ...,
    ),
  ),
);