A sale is a distinct model from a gift or a Plantare (reproduction
commitment): a recorded seed sale or purchase with an optional price in
ANY currency — €, Ğ1, time, or none yet. Mirrors the Plantare feature.
- schema v10: Sales table (SyncColumns), guarded createTable migration,
schema dump + generated schema_v10 for the migration round-trip test
- enum SaleDirection { iSold, iBought }
- VarietyRepository: create/watch/watchForVariety/delete + backup
export/exportForSync/import (LWW-by-HLC, tombstones)
- InventorySnapshot.sales + JSON codec encode/decode (round-trips amount,
currency, counterparty)
- UI: SalesScreen (/sales), sale sheet, drawer entry, variety-detail
action beside 'add Plantare'; money hides a trailing .0
- i18n sale block + menu.sales in en/es/pt/ast
- tests: sales repo test (5) incl. Ğ1/price-less/backup round-trip;
Sales screen added to the small-screen overflow guard
Also harden the overflow guard: swallow the headless engine's image
resource service PNG-decode errors (unrelated to layout) while still
failing on real RenderFlex overflows, so home/about stop reporting
false failures.
165 lines
5.4 KiB
Dart
165 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../data/variety_repository.dart';
|
|
import '../db/enums.dart';
|
|
import '../i18n/strings.g.dart';
|
|
import 'theme.dart';
|
|
|
|
/// Opens the "record a sale" sheet. Optionally pre-attached to [varietyId]
|
|
/// (when opened from a variety's detail).
|
|
Future<void> showSaleSheet(
|
|
BuildContext context, {
|
|
required VarietyRepository repository,
|
|
String? varietyId,
|
|
}) {
|
|
return showModalBottomSheet<void>(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
builder: (_) => _SaleSheet(repository: repository, varietyId: varietyId),
|
|
);
|
|
}
|
|
|
|
class _SaleSheet extends StatefulWidget {
|
|
const _SaleSheet({required this.repository, this.varietyId});
|
|
|
|
final VarietyRepository repository;
|
|
final String? varietyId;
|
|
|
|
@override
|
|
State<_SaleSheet> createState() => _SaleSheetState();
|
|
}
|
|
|
|
class _SaleSheetState extends State<_SaleSheet> {
|
|
SaleDirection _direction = SaleDirection.iSold;
|
|
final _counterparty = TextEditingController();
|
|
final _amount = TextEditingController();
|
|
final _currency = TextEditingController();
|
|
final _note = TextEditingController();
|
|
bool _saving = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_counterparty.dispose();
|
|
_amount.dispose();
|
|
_currency.dispose();
|
|
_note.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String? _nullIfBlank(String s) => s.trim().isEmpty ? null : s.trim();
|
|
|
|
Future<void> _save() async {
|
|
setState(() => _saving = true);
|
|
await widget.repository.createSale(
|
|
direction: _direction,
|
|
varietyId: widget.varietyId,
|
|
counterparty: _nullIfBlank(_counterparty.text),
|
|
amount: double.tryParse(_amount.text.trim().replaceAll(',', '.')),
|
|
currency: _nullIfBlank(_currency.text),
|
|
note: _nullIfBlank(_note.text),
|
|
);
|
|
if (mounted) Navigator.of(context).pop();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
return Padding(
|
|
padding: EdgeInsets.only(
|
|
left: 16,
|
|
right: 16,
|
|
top: 16,
|
|
bottom: MediaQuery.of(context).viewInsets.bottom + 16,
|
|
),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(t.sale.add, style: Theme.of(context).textTheme.titleLarge),
|
|
const SizedBox(height: 4),
|
|
Text(t.sale.help,
|
|
style: const TextStyle(color: seedMuted, fontSize: 13)),
|
|
const SizedBox(height: 16),
|
|
Text(t.sale.direction,
|
|
style: Theme.of(context).textTheme.labelLarge),
|
|
const SizedBox(height: 6),
|
|
for (final d in SaleDirection.values)
|
|
ListTile(
|
|
key: Key('sale.direction.${d.name}'),
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: Icon(
|
|
_direction == d
|
|
? Icons.radio_button_checked
|
|
: Icons.radio_button_unchecked,
|
|
color: _direction == d ? seedGreen : seedMuted,
|
|
),
|
|
title: Text(
|
|
d == SaleDirection.iSold ? t.sale.iSold : t.sale.iBought),
|
|
onTap: () => setState(() => _direction = d),
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
key: const Key('sale.counterparty'),
|
|
controller: _counterparty,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: InputDecoration(
|
|
labelText: t.sale.counterparty,
|
|
helperText: t.sale.counterpartyHint,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Row(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
key: const Key('sale.amount'),
|
|
controller: _amount,
|
|
keyboardType: const TextInputType.numberWithOptions(
|
|
decimal: true),
|
|
decoration: InputDecoration(
|
|
labelText: t.sale.amount,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: TextField(
|
|
key: const Key('sale.currency'),
|
|
controller: _currency,
|
|
decoration: InputDecoration(
|
|
labelText: t.sale.currency,
|
|
helperText: t.sale.currencyHint,
|
|
helperMaxLines: 2,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
key: const Key('sale.note'),
|
|
controller: _note,
|
|
minLines: 1,
|
|
maxLines: 3,
|
|
decoration: InputDecoration(
|
|
labelText: t.sale.note,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
FilledButton(
|
|
key: const Key('sale.save'),
|
|
onPressed: _saving ? null : _save,
|
|
child: Text(t.sale.save),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|