DataTable 扩展到窗口,如果溢出也会水平滚动
我正在尝试创建一个 DataTable,它将像 DataTable 通常那样扩展到其父级的可用宽度,但如果屏幕上没有足够的水平空间,它应该允许水平滚动。就目前情况而言,我只能做其中之一。问题是当我允许表格水平滚动时,单元格不再扩展。当我允许单元格水平扩展时,如果表格不适合,就会从页面上切掉。
下面的示例代码是表格版本,其中单元格不会展开,但可以滚动。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController verticalController = ScrollController();
ScrollController horizontalController = ScrollController();
bool maximized = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Scrollbar(
controller: verticalController,
isAlwaysShown: true,
child: SingleChildScrollView(
controller: verticalController,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
Text('Table Name'),
Container(
width: double.infinity,
child: Scrollbar(
isAlwaysShown: true,
controller: horizontalController,
child: SingleChildScrollView(
controller: horizontalController,
scrollDirection: Axis.horizontal,
child: DataTable(
dataRowHeight: 50,
columnSpacing: 20,
horizontalMargin: 0,
columns: List<DataColumn>.generate(
10,
(index) => DataColumn(
label: Text("Column" + index.toString()))),
rows: List<DataRow>.generate(10, (rowIndex) {
if (maximized) {
return DataRow(
cells: List<DataCell>.generate(
10,
(cellIndex) => DataCell(
Container(
width: 100,
child: Column(
children: [
Text("row" + rowIndex.toString()),
Text("Cell" + cellIndex.toString()),
],
),
),
),
),
);
} else {
return DataRow(
cells: List<DataCell>.generate(
10,
(cellIndex) => DataCell(
Container(
child: Column(
children: [
Text("row" + rowIndex.toString()),
Text("Cell" + cellIndex.toString()),
],
),
),
),
),
);
}
})),
),
),
),
],
),
),
),
));
}
}
I'm trying to create a DataTable that will expand to the available width of its parent as DataTables do normally, but also if there isn't enough horizontal space on the screen it should allow horizontal scrolling. As it stands I'm only able to do one or the other. The problem is when I allow the table to scroll horizontally, the cells no longer expand. And when I allow the cells to expand horizontally, the table will be cut off of the page if it doesn't fit.
Sample code below is of the table version where the cells won't expand, but scrolling works.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController verticalController = ScrollController();
ScrollController horizontalController = ScrollController();
bool maximized = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Scrollbar(
controller: verticalController,
isAlwaysShown: true,
child: SingleChildScrollView(
controller: verticalController,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: [
Text('Table Name'),
Container(
width: double.infinity,
child: Scrollbar(
isAlwaysShown: true,
controller: horizontalController,
child: SingleChildScrollView(
controller: horizontalController,
scrollDirection: Axis.horizontal,
child: DataTable(
dataRowHeight: 50,
columnSpacing: 20,
horizontalMargin: 0,
columns: List<DataColumn>.generate(
10,
(index) => DataColumn(
label: Text("Column" + index.toString()))),
rows: List<DataRow>.generate(10, (rowIndex) {
if (maximized) {
return DataRow(
cells: List<DataCell>.generate(
10,
(cellIndex) => DataCell(
Container(
width: 100,
child: Column(
children: [
Text("row" + rowIndex.toString()),
Text("Cell" + cellIndex.toString()),
],
),
),
),
),
);
} else {
return DataRow(
cells: List<DataCell>.generate(
10,
(cellIndex) => DataCell(
Container(
child: Column(
children: [
Text("row" + rowIndex.toString()),
Text("Cell" + cellIndex.toString()),
],
),
),
),
),
);
}
})),
),
),
),
],
),
),
),
));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试给出上面的 SingleChildScrollView 属性吗:
希望我明白问题是什么
你可以尝试一下希望有帮助
Can You try to give the upper SingleChildScrollView property of:
Hope I understand what is the problem
You can give it a try hope it helps
我可以通过在桌子周围使用约束框并将最小宽度设置为屏幕上的可用宽度来解决此问题。这仍然允许表格水平扩展,同时保持宽度至少与可用屏幕宽度一样大。
I was able to fix this issue by using a constrained box around my table and setting the minimum width to the available width on the screen. This still allows the table to expand horizontally while keeping the width at least as large as the available screen width.
有点晚了,但如果有人正在搜索这个,请将 DataTable 包装在
SingleChildScrollView
中,并将滚动方向设置为Axis.horizontal
。您必须将
列
的最小宽度设置为Little late, but if someone is searching for this, wrap DataTable in a
SingleChildScrollView
with scrollDirection asAxis.horizontal
.You Must set minium width for the
columns
as