cake_wallet/lib/src/widgets/blockchain_height_widget.dart

114 lines
3.2 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/utils/date_picker.dart';
2020-01-04 19:31:52 +00:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:cake_wallet/generated/i18n.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/monero/get_height_by_date.dart';
import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
2020-01-04 19:31:52 +00:00
class BlockchainHeightWidget extends StatefulWidget {
2020-06-20 07:10:00 +00:00
BlockchainHeightWidget({GlobalKey key, this.onHeightChange})
: super(key: key);
final Function(int) onHeightChange;
2020-01-04 19:31:52 +00:00
@override
State<StatefulWidget> createState() => BlockchainHeightState();
}
class BlockchainHeightState extends State<BlockchainHeightWidget> {
final dateController = TextEditingController();
final restoreHeightController = TextEditingController();
2020-06-20 07:10:00 +00:00
2020-01-04 19:31:52 +00:00
int get height => _height;
int _height = 0;
@override
void initState() {
2020-06-20 07:10:00 +00:00
restoreHeightController.addListener(() {
try {
_changeHeight(restoreHeightController.text != null &&
restoreHeightController.text.isNotEmpty
2020-01-04 19:31:52 +00:00
? int.parse(restoreHeightController.text)
: 0);
2020-06-20 07:10:00 +00:00
} catch (_) {
_changeHeight(0);
}
});
2020-01-04 19:31:52 +00:00
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Flexible(
child: Container(
padding: EdgeInsets.only(top: 20.0, bottom: 10.0),
child: BaseTextFormField(
2020-01-04 19:31:52 +00:00
controller: restoreHeightController,
keyboardType: TextInputType.numberWithOptions(
signed: false, decimal: false),
hintText: S.of(context).widgets_restore_from_blockheight,
)
2020-01-04 19:31:52 +00:00
))
],
),
Padding(
padding: EdgeInsets.only(top: 15, bottom: 15),
child: Text(
S.of(context).widgets_or,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w500,
color: Theme.of(context).primaryTextTheme.title.color),
2020-01-04 19:31:52 +00:00
),
),
Row(
children: <Widget>[
Flexible(
child: Container(
child: InkWell(
onTap: () => _selectDate(context),
child: IgnorePointer(
child: BaseTextFormField(
2020-01-04 19:31:52 +00:00
controller: dateController,
hintText: S.of(context).widgets_restore_from_date,
)
2020-01-04 19:31:52 +00:00
),
),
))
],
),
],
);
}
Future _selectDate(BuildContext context) async {
final now = DateTime.now();
final date = await getDate(
2020-01-04 19:31:52 +00:00
context: context,
initialDate: now.subtract(Duration(days: 1)),
firstDate: DateTime(2014, DateTime.april),
lastDate: now);
if (date != null) {
final height = getHeigthByDate(date: date);
setState(() {
dateController.text = DateFormat('yyyy-MM-dd').format(date);
restoreHeightController.text = '$height';
2020-06-20 07:10:00 +00:00
_changeHeight(height);
2020-01-04 19:31:52 +00:00
});
}
}
2020-06-20 07:10:00 +00:00
void _changeHeight(int height) {
_height = height;
widget.onHeightChange?.call(height);
}
2020-01-04 19:31:52 +00:00
}