git-touch-android-ios-app/lib/widgets/table_view.dart

128 lines
3.4 KiB
Dart
Raw Normal View History

2019-02-08 16:20:28 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2019-09-04 16:37:22 +02:00
import 'package:git_touch/utils/utils.dart';
2019-10-02 08:58:11 +02:00
import 'package:git_touch/widgets/border_view.dart';
2019-09-04 16:37:22 +02:00
import 'package:primer/primer.dart';
2019-02-08 16:20:28 +01:00
import 'link.dart';
2019-09-21 17:54:25 +02:00
class TableViewHeader extends StatelessWidget {
final String title;
TableViewHeader(this.title);
@override
Widget build(BuildContext context) {
return Container(
color: PrimerColors.gray100,
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
child: Text(
title.toUpperCase(),
style: TextStyle(color: PrimerColors.gray600, fontSize: 13),
),
);
}
}
2019-02-08 16:20:28 +01:00
class TableViewItem {
2019-09-04 16:59:33 +02:00
final Widget text;
2019-09-14 17:48:01 +02:00
final IconData leftIconData;
2019-09-04 16:59:33 +02:00
final Widget leftWidget;
final Widget rightWidget;
2019-02-08 16:20:28 +01:00
final void Function() onTap;
2019-09-23 18:34:51 +02:00
final String url;
2019-02-08 16:20:28 +01:00
final WidgetBuilder screenBuilder;
2019-09-14 17:48:01 +02:00
final bool hideRightChevron;
2019-02-08 16:20:28 +01:00
TableViewItem({
2019-09-14 18:01:15 +02:00
@required this.text,
2019-09-14 17:48:01 +02:00
this.leftIconData,
2019-09-04 16:59:33 +02:00
this.leftWidget,
this.rightWidget,
2019-02-08 16:20:28 +01:00
this.onTap,
2019-09-23 18:34:51 +02:00
this.url,
2019-02-08 16:20:28 +01:00
this.screenBuilder,
2019-09-14 17:48:01 +02:00
this.hideRightChevron = false,
}) : assert(leftIconData == null || leftWidget == null);
2019-02-08 16:20:28 +01:00
}
class TableView extends StatelessWidget {
2019-09-04 16:37:22 +02:00
final String headerText;
2019-09-14 18:01:15 +02:00
final Iterable<TableViewItem> items;
2019-09-15 09:08:09 +02:00
final bool hasIcon;
2019-02-08 16:20:28 +01:00
2019-09-15 09:08:09 +02:00
double get _leftPadding => hasIcon ? 44 : 12;
TableView({this.headerText, @required this.items, this.hasIcon = false});
2019-09-04 16:37:22 +02:00
2019-09-05 11:58:14 +02:00
Widget _buildItem(TableViewItem item) {
2019-09-14 11:19:33 +02:00
if (item == null) return null;
2019-09-15 09:08:09 +02:00
var leftWidget = item.leftWidget;
if (leftWidget == null && hasIcon) {
leftWidget = Icon(
item.leftIconData,
color: PrimerColors.blue500,
size: 18,
);
}
2019-09-14 18:01:15 +02:00
// Container(
// width: 24,
// height: 24,
// // decoration: BoxDecoration(
// // borderRadius: BorderRadius.circular(4), color: PrimerColors.blue400),
// child: Icon(iconData, size: 24, color: PrimerColors.gray600),
// )
2019-09-14 17:48:01 +02:00
var widget = DefaultTextStyle(
style: TextStyle(fontSize: 16, color: PrimerColors.gray900),
overflow: TextOverflow.ellipsis,
2019-09-14 20:05:34 +02:00
child: Container(
2019-09-14 17:48:01 +02:00
height: 44,
2019-09-14 20:05:34 +02:00
child: Row(
children: [
2019-09-15 09:08:09 +02:00
SizedBox(width: _leftPadding, child: leftWidget),
2019-09-14 20:05:34 +02:00
Expanded(child: item.text),
if (item.rightWidget != null) ...[
DefaultTextStyle(
style: TextStyle(fontSize: 16, color: PrimerColors.gray500),
child: item.rightWidget,
),
SizedBox(width: 6)
],
2019-09-23 18:34:51 +02:00
if ((item.onTap != null ||
item.screenBuilder != null ||
item.url != null) &&
2019-09-14 20:05:34 +02:00
!item.hideRightChevron)
Icon(CupertinoIcons.right_chevron,
size: 20, color: PrimerColors.gray300)
else
SizedBox(width: 2),
SizedBox(width: 8),
2019-09-14 18:01:15 +02:00
],
2019-09-14 20:05:34 +02:00
),
2019-09-14 17:48:01 +02:00
),
2019-09-05 11:58:14 +02:00
);
return Link(
onTap: item.onTap,
screenBuilder: item.screenBuilder,
2019-09-23 18:34:51 +02:00
url: item.url,
2019-09-05 11:58:14 +02:00
child: widget,
);
}
2019-02-08 16:20:28 +01:00
@override
Widget build(BuildContext context) {
2019-09-04 16:37:22 +02:00
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
2019-09-21 17:54:25 +02:00
if (headerText != null) TableViewHeader(headerText),
2019-09-14 17:48:01 +02:00
borderView,
2019-09-15 09:08:09 +02:00
...join(BorderView(leftPadding: _leftPadding),
items.map(_buildItem).toList()),
2019-09-14 17:48:01 +02:00
borderView,
2019-09-04 16:37:22 +02:00
],
2019-02-08 16:20:28 +01:00
);
}
}