Rework quantities the way seeds are actually described — a number of an informal
unit — and let a lot hold seeds or plants (plantel).
Quantity model (commons_core):
- Quantity is now { kind, count?, label } — "3 cobs", "2 packets", "a few".
Count is optional; uncountable vibes (a few, some, pinch) carry no number.
- QuantityKind reorganised into an ascending scale: vibes → containers
(teaspoon/spoon/cup/jar/sack) → seed & fruit forms → plant units, each tagged
countable/not. Stored by name; count reuses the existing numeric column.
Lot type (schema v2):
- Lots gain a `type` (seed | plant); germination stays meaningful for seeds.
schemaVersion 2 with a from1To2 migration (adds the column), drift_schema_v2
exported, migration test covers v1→v2.
UI:
- New QuantityPicker: a horizontal scale of large seed-glyph icons + a number
stepper (shown only for countable units), plus a seed/plant SegmentedButton.
Used in quick-add and add-lot. Lot lines render "3 cobs" / "a few".
- i18n: unit singular/plural (ES/EN, Weblate-friendly), lot-type labels.
Build: slang moved to its CLI (disabled in build_runner to avoid a multi-file
collision); CI runs `dart run slang` before build_runner. 38 tests green,
Linux migrates the existing v1 DB and runs.
175 lines
5.5 KiB
Dart
175 lines
5.5 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
import '../data/variety_repository.dart';
|
|
import '../i18n/strings.g.dart';
|
|
import '../state/quick_add_cubit.dart';
|
|
import 'quantity_picker.dart';
|
|
|
|
/// Picks a photo and returns its bytes (or null if cancelled). Injected so
|
|
/// widget tests can supply a fake instead of the camera plugin.
|
|
typedef PhotoPicker = Future<Uint8List?> Function();
|
|
|
|
Future<void> showQuickAddSheet(
|
|
BuildContext context, {
|
|
required VarietyRepository repository,
|
|
PhotoPicker? photoPicker,
|
|
}) {
|
|
return showModalBottomSheet<void>(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
builder: (_) => BlocProvider(
|
|
create: (_) => QuickAddCubit(repository),
|
|
child: QuickAddSheet(photoPicker: photoPicker ?? _cameraPhotoPicker),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<Uint8List?> _cameraPhotoPicker() async {
|
|
final file = await ImagePicker().pickImage(
|
|
source: ImageSource.camera,
|
|
maxWidth: 1280,
|
|
imageQuality: 80,
|
|
);
|
|
return file?.readAsBytes();
|
|
}
|
|
|
|
class QuickAddSheet extends StatelessWidget {
|
|
const QuickAddSheet({required this.photoPicker, super.key});
|
|
|
|
final PhotoPicker photoPicker;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
return BlocConsumer<QuickAddCubit, QuickAddState>(
|
|
listenWhen: (prev, curr) => !prev.submitted && curr.submitted,
|
|
listener: (context, state) => Navigator.of(context).pop(),
|
|
builder: (context, state) {
|
|
final cubit = context.read<QuickAddCubit>();
|
|
return Padding(
|
|
padding: EdgeInsets.only(
|
|
left: 16,
|
|
right: 16,
|
|
top: 16,
|
|
bottom: MediaQuery.of(context).viewInsets.bottom + 16,
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
t.quickAdd.title,
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
key: const Key('quickAdd.labelField'),
|
|
autofocus: true,
|
|
textInputAction: TextInputAction.done,
|
|
decoration: InputDecoration(
|
|
labelText: t.quickAdd.labelField,
|
|
errorText: state.showLabelError
|
|
? t.quickAdd.labelRequired
|
|
: null,
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
onChanged: cubit.labelChanged,
|
|
onSubmitted: (_) => cubit.submit(),
|
|
),
|
|
const SizedBox(height: 16),
|
|
LotTypeSelector(
|
|
value: state.lotType,
|
|
onChanged: (type) {
|
|
cubit
|
|
..setLotType(type)
|
|
..setQuantity(null);
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
t.quickAdd.quantity,
|
|
style: Theme.of(context).textTheme.labelLarge,
|
|
),
|
|
const SizedBox(height: 8),
|
|
QuantityPicker(
|
|
type: state.lotType,
|
|
value: state.quantity,
|
|
onChanged: cubit.setQuantity,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: TextButton.icon(
|
|
onPressed: cubit.toggleExpanded,
|
|
icon: Icon(
|
|
state.expanded ? Icons.expand_less : Icons.expand_more,
|
|
),
|
|
label: Text(t.quickAdd.more),
|
|
),
|
|
),
|
|
if (state.expanded) _MoreSection(photoPicker: photoPicker),
|
|
const SizedBox(height: 8),
|
|
Row(
|
|
children: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: Text(t.quickAdd.cancel),
|
|
),
|
|
const Spacer(),
|
|
FilledButton(
|
|
key: const Key('quickAdd.save'),
|
|
onPressed: state.submitting ? null : cubit.submit,
|
|
child: Text(t.quickAdd.save),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
class _MoreSection extends StatelessWidget {
|
|
const _MoreSection({required this.photoPicker});
|
|
|
|
final PhotoPicker photoPicker;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final t = context.t;
|
|
final state = context.watch<QuickAddCubit>().state;
|
|
final cubit = context.read<QuickAddCubit>();
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const SizedBox(height: 8),
|
|
if (state.photoBytes != null)
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.circular(8),
|
|
child: Image.memory(
|
|
state.photoBytes!,
|
|
height: 120,
|
|
fit: BoxFit.cover,
|
|
),
|
|
),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: OutlinedButton.icon(
|
|
onPressed: () async {
|
|
final bytes = await photoPicker();
|
|
if (bytes != null) cubit.photoPicked(bytes);
|
|
},
|
|
icon: const Icon(Icons.photo_camera_outlined),
|
|
label: Text(t.quickAdd.addPhoto),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|