flutter_demo_gps/lib/widgets/two_parts.dart

68 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class TwoParts extends StatelessWidget {
final String title;
final Localizable value;
factory TwoParts.fromInt(String title, int value) =>
TwoParts(title: title, value: _IntHolder(value));
factory TwoParts.fromDouble(String title, double value) =>
TwoParts(title: title, value: _DoubleHolder(value));
factory TwoParts.fromString(String title, String value) =>
TwoParts(title: title, value: _StringHolder(value));
factory TwoParts.fromMillisecondDateTime(String title, int value) =>
TwoParts(title: title, value: _DateHolder(value));
const TwoParts({
required this.title,
required this.value,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
child: Row(children: [
Expanded(flex: 1, child: Text(title)),
Expanded(
flex: 1, child: Text(value.format(Localizations.localeOf(context))))
]));
}
}
mixin Localizable {
String format(Locale locale);
}
class _StringHolder implements Localizable {
final String _string;
_StringHolder(this._string);
@override
String format(Locale locale) => _string;
}
class _IntHolder implements Localizable {
final int _int;
_IntHolder(this._int);
@override
String format(Locale locale) =>
NumberFormat("#", locale.toString()).format(_int);
}
class _DoubleHolder implements Localizable {
final double _double;
_DoubleHolder(this._double);
@override
String format(Locale locale) =>
NumberFormat("##0.0#", locale.toString()).format(_double);
}
class _DateHolder implements Localizable {
final DateTime _timestamp;
_DateHolder(int i) : _timestamp = DateTime.fromMicrosecondsSinceEpoch(i);
@override
String format(Locale locale) =>
"${DateFormat.yMMMMEEEEd(locale.toString()).format(_timestamp)}\n${DateFormat.jms(locale.toString()).format(_timestamp)}";
}