M A*_*A F 5 datatable flutter flutter-layout
目标和问题:我正在尝试通过 DataTable 小部件为我的应用程序创建一个包含逐项的结帐页面。但我得到了 13 像素的溢出错误:
右侧的 RenderFlex 溢出了 23 个像素。相关的导致错误的小部件是 DataTable
造成此问题的原因是什么以及补救措施是什么?
图片:
代码:
Expanded(
flex: 8,
child: Container(
padding: EdgeInsets.all(10),
child: DataTable(
columns: [
DataColumn(
label: Text(
'#',
style: GoogleFonts.ubuntuMono(
color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'Menu Item',
style: GoogleFonts.ubuntuMono(
color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'Item Price',
style: GoogleFonts.ubuntuMono(
color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'-',
style: GoogleFonts.ubuntuMono(
color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
],
rows: [
DataRow(cells: [
DataCell(
Text(
'1',
style: GoogleFonts.ubuntuMono(
fontStyle: FontStyle.italic,
fontSize: 15,
color: colorPalette.chooseColor('darkGrey'),
),
),
),
DataCell(
Text(
'dish name',
style: GoogleFonts.ubuntuMono(
fontStyle: FontStyle.italic,
fontSize: 15,
color: colorPalette.chooseColor('darkGrey'),
),
),
),
DataCell(
Center(
child: Text('\$\$\$',
style: GoogleFonts.ubuntuMono(
fontWeight: FontWeight.bold,
fontSize: 15,
color: colorPalette.chooseColor('darkGrey'),
))),
),
DataCell(
IconButton(
icon: Icon(
Icons.cancel_sharp,
color: colorPalette.chooseColor('darkOrange'),
),
onPressed: () {},
),
),
]),
],
),
),
),
Run Code Online (Sandbox Code Playgroud)
您可以复制粘贴运行下面的完整代码
您可以使用SingleChildScrollView包装Axis.horizontal代码DataTable
片段
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: [
Run Code Online (Sandbox Code Playgroud)
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
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> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 8,
child: Container(
padding: EdgeInsets.all(10),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: [
DataColumn(
label: Text(
'#',
style: GoogleFonts.ubuntuMono(
//color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'Menu Item',
style: GoogleFonts.ubuntuMono(
//color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'Item Price',
style: GoogleFonts.ubuntuMono(
//color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
DataColumn(
label: Text(
'-',
style: GoogleFonts.ubuntuMono(
//color: colorPalette.chooseColor('orange'),
fontSize: 17),
),
),
],
rows: [
DataRow(cells: [
DataCell(
Text(
'1',
style: GoogleFonts.ubuntuMono(
fontStyle: FontStyle.italic,
fontSize: 15,
//color: colorPalette.chooseColor('darkGrey'),
),
),
),
DataCell(
Text(
'dish name',
style: GoogleFonts.ubuntuMono(
fontStyle: FontStyle.italic,
fontSize: 15,
//color: colorPalette.chooseColor('darkGrey'),
),
),
),
DataCell(
Center(
child: Text('\$\$\$',
style: GoogleFonts.ubuntuMono(
fontWeight: FontWeight.bold,
fontSize: 15,
//color: colorPalette.chooseColor('darkGrey'),
))),
),
DataCell(
IconButton(
icon: Icon(
Icons.cancel_sharp,
//color: colorPalette.chooseColor('darkOrange'),
),
onPressed: () {},
),
),
]),
],
),
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6130 次 |
| 最近记录: |