我寫了一個硬編碼的 50 行。現在我需要優化代碼。
根據預期的輸出,第一行應該有一個文本輸入,它只接受 int 值并說我輸入 10,然后應該動態創建 10 行。
https://github.com/AafreenKhan040896/skillsTable/blob/main/lib/main.dart
這是我正在嘗試創建的小部件。
uj5u.com熱心網友回復:
我對您的SkillTable課程進行了一些優化:
class SkillsTable extends StatelessWidget {
const SkillsTable({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// ignore: prefer_typing_uninitialized_variables
var value;
return Scaffold(
appBar:
AppBar(centerTitle: true, title: const Text('Skills & Experince')),
body: Row(
children: [
Container(
width: (MediaQuery.of(context).size.width / 2) - 30,
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration:
BoxDecoration(border: Border.all(color: Colors.black)),
child: const Text("Hello"),
),
Container(
width: (MediaQuery.of(context).size.width / 2) - 30,
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
showBottomBorder: true,
dividerThickness: 2.0,
headingRowColor: MaterialStateColor.resolveWith(
(states) => Colors.cyanAccent),
headingRowHeight: 70,
dataRowHeight: 50,
columnSpacing:
(MediaQuery.of(context).size.width / 67) * 3,
columns: [
_getDataColumn('Skills'),
_getDataColumn('Months'),
_getDataColumn('SelfScore'),
_getDataColumn('RightScore'),
_getDataColumn('Notes'),
],
rows: _getListRows(50, value),
),
),
),
),
],
));
}
DataColumn _getDataColumn(String name) {
return DataColumn(
label: Text(
name,
style: TextStyle(fontWeight: FontWeight.bold),
));
}
DataRow _dataRowEmpty() {
return DataRow(cells: <DataCell>[
DataCell(Text('')),
DataCell(Text('')),
DataCell(Text('')),
DataCell(Text('')),
DataCell(Text('')),
]);
}
DataRow _dataRow(value) {
return DataRow(cells: <DataCell>[
const DataCell(MyDropDownWidget()),
DataCell(
TextFormField(
initialValue: '$value',
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
),
showEditIcon: true),
DataCell(
TextFormField(
initialValue: '$value',
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
),
showEditIcon: true),
const DataCell(Text('')),
DataCell(
TextFormField(
initialValue: '$value',
keyboardType: TextInputType.multiline,
maxLength: 100,
),
showEditIcon: true),
]);
}
List<DataRow> _getListRows(int rows, dynamic value) {
List<DataRow> list = [];
list.add(_dataRowEmpty());
for (int i = 0; i < rows; i ){
list.add(_dataRow(value));
}
return list;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330706.html
