Skip to content
Prev Previous commit
Next Next commit
Unintended changes
  • Loading branch information
Piinks committed Mar 29, 2024
commit 3b6d7ce7b54f3de6fe681df4ac29d0f240aae7b2
140 changes: 106 additions & 34 deletions packages/two_dimensional_scrollables/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,67 +41,114 @@ class TableExample extends StatefulWidget {
}

class _TableExampleState extends State<TableExample> {
int counter = 0;
int get _columnCount {
return switch (counter % 3) {
0 => 20,
1 => 30,
2 => 10,
_ => 500,
};
}
late final ScrollController _verticalController = ScrollController();
int _rowCount = 20;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Row count $_columnCount'),
),
body: TableView.builder(
cellBuilder: _buildCell,
// columnCount: 20,
columnBuilder: _buildColumnSpan,
// rowCount: _rowCount,
rowBuilder: _buildRowSpan,
title: const Text('Table Example'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
print('press');
counter++;
});
},
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: TableView.builder(
verticalDetails:
ScrollableDetails.vertical(controller: _verticalController),
cellBuilder: _buildCell,
columnCount: 20,
columnBuilder: _buildColumnSpan,
rowCount: _rowCount,
rowBuilder: _buildRowSpan,
),
),
persistentFooterButtons: <Widget>[
TextButton(
onPressed: () {
_verticalController.jumpTo(0);
},
child: const Text('Jump to Top'),
),
TextButton(
onPressed: () {
_verticalController
.jumpTo(_verticalController.position.maxScrollExtent);
},
child: const Text('Jump to Bottom'),
),
TextButton(
onPressed: () {
setState(() {
_rowCount += 10;
});
},
child: const Text('Add 10 Rows'),
),
],
);
}

TableViewCell _buildCell(BuildContext context, TableVicinity vicinity) {
print(vicinity);
return TableViewCell(
child: Center(
child: Text('Tile c: ${vicinity.column}, r: ${vicinity.row}'),
),
);
}

TableSpan? _buildColumnSpan(int index) {
if (index > _columnCount) {
return null;
}
TableSpan _buildColumnSpan(int index) {
const TableSpanDecoration decoration = TableSpanDecoration(
border: TableSpanBorder(
trailing: BorderSide(),
),
);

return const TableSpan(
switch (index % 5) {
case 0:
return TableSpan(
foregroundDecoration: decoration,
extent: const FixedTableSpanExtent(100),
onEnter: (_) => print('Entered column $index'),
recognizerFactories: <Type, GestureRecognizerFactory>{
TapGestureRecognizer:
GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
() => TapGestureRecognizer(),
(TapGestureRecognizer t) =>
t.onTap = () => print('Tap column $index'),
),
},
);
case 1:
return TableSpan(
foregroundDecoration: decoration,
extent: FixedTableSpanExtent(100),
extent: const FractionalTableSpanExtent(0.5),
onEnter: (_) => print('Entered column $index'),
cursor: SystemMouseCursors.contextMenu,
);
case 2:
return TableSpan(
foregroundDecoration: decoration,
extent: const FixedTableSpanExtent(120),
onEnter: (_) => print('Entered column $index'),
);
case 3:
return TableSpan(
foregroundDecoration: decoration,
extent: const FixedTableSpanExtent(145),
onEnter: (_) => print('Entered column $index'),
);
case 4:
return TableSpan(
foregroundDecoration: decoration,
extent: const FixedTableSpanExtent(200),
onEnter: (_) => print('Entered column $index'),
);
}
throw AssertionError(
'This should be unreachable, as every index is accounted for in the switch clauses.');
}

TableSpan? _buildRowSpan(int index) {

TableSpan _buildRowSpan(int index) {
final TableSpanDecoration decoration = TableSpanDecoration(
color: index.isEven ? Colors.purple[100] : null,
border: const TableSpanBorder(
Expand All @@ -110,9 +157,34 @@ class _TableExampleState extends State<TableExample> {
),
),
);
return TableSpan(

switch (index % 3) {
case 0:
return TableSpan(
backgroundDecoration: decoration,
extent: const FixedTableSpanExtent(50),
recognizerFactories: <Type, GestureRecognizerFactory>{
TapGestureRecognizer:
GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
() => TapGestureRecognizer(),
(TapGestureRecognizer t) =>
t.onTap = () => print('Tap row $index'),
),
},
);
case 1:
return TableSpan(
backgroundDecoration: decoration,
extent: const FixedTableSpanExtent(65),
cursor: SystemMouseCursors.click,
);
case 2:
return TableSpan(
backgroundDecoration: decoration,
extent: const FractionalTableSpanExtent(0.15),
);
}
throw AssertionError(
'This should be unreachable, as every index is accounted for in the switch clauses.');
}
}