[FilmEditScreen] separated add and edit blocs

This commit is contained in:
Vadim 2024-10-23 16:20:42 +02:00
parent e781bb38ac
commit a6f319bf99
6 changed files with 23 additions and 6 deletions

View file

@ -151,8 +151,11 @@
}
},
"close": "Close",
"addFilmTitle": "Add film",
"editFilmTitle": "Edit film",
"filmFormula": "Formula",
"filmFormulaExponential": "T=t^Rf",
"filmFormulaExponentialRf": "Rf",
"filmFormulaExponentialRfPlaceholder": "1.3",
"name": "Name"
}

View file

@ -4,10 +4,13 @@ import 'package:lightmeter/screens/film_edit/state_film_edit.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
static const _defaultFilm = FilmExponential(name: '', iso: 100, exponent: 1.3);
final FilmExponential _originalFilm;
FilmExponential _newFilm;
FilmEditBloc(FilmExponential film)
factory FilmEditBloc(FilmExponential? film) => film != null ? FilmEditBloc._(film) : FilmEditBloc._(_defaultFilm);
FilmEditBloc._(FilmExponential film)
: _originalFilm = film,
_newFilm = film,
super(
@ -16,6 +19,7 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
isoValue: IsoValue.values.firstWhere((element) => element.value == film.iso),
exponent: film.exponent,
canSave: false,
isEdit: film != _defaultFilm,
),
) {
on<FilmEditEvent>(
@ -42,6 +46,7 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
isoValue: state.isoValue,
exponent: state.exponent,
canSave: _canSave(event.name, state.exponent),
isEdit: state.isEdit,
),
);
}
@ -54,6 +59,7 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
isoValue: event.iso,
exponent: state.exponent,
canSave: _canSave(state.name, state.exponent),
isEdit: state.isEdit,
),
);
}
@ -68,6 +74,7 @@ class FilmEditBloc extends Bloc<FilmEditEvent, FilmEditState> {
isoValue: state.isoValue,
exponent: event.exponent,
canSave: _canSave(state.name, event.exponent),
isEdit: state.isEdit,
),
);
}

View file

@ -5,9 +5,9 @@ import 'package:lightmeter/screens/film_edit/screen_film_edit.dart';
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class FilmEditFlow extends StatelessWidget {
final FilmExponential film;
final FilmExponential? film;
const FilmEditFlow(this.film, {super.key});
const FilmEditFlow({this.film, super.key});
@override
Widget build(BuildContext context) {

View file

@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/res/dimens.dart';
@ -24,8 +23,8 @@ class _FilmEditScreenState extends State<FilmEditScreen> {
Widget build(BuildContext context) {
return SliverScreen(
title: BlocBuilder<FilmEditBloc, FilmEditState>(
buildWhen: (previous, current) => previous.name != current.name,
builder: (_, state) => Text(state.name),
buildWhen: (previous, current) => false,
builder: (context, state) => Text(state.isEdit ? S.of(context).editFilmTitle : S.of(context).addFilmTitle),
),
appBarActions: [
BlocBuilder<FilmEditBloc, FilmEditState>(
@ -97,6 +96,8 @@ class _NameFieldBuilder extends StatelessWidget {
buildWhen: (previous, current) => previous.name != current.name,
builder: (context, state) => LightmeterTextField(
initialValue: state.name,
maxLength: 48,
hintText: S.of(context).name,
onChanged: (value) {
context.read<FilmEditBloc>().add(FilmEditNameChangedEvent(value));
},

View file

@ -5,11 +5,13 @@ class FilmEditState {
final IsoValue isoValue;
final double? exponent;
final bool canSave;
final bool isEdit;
const FilmEditState({
required this.name,
required this.isoValue,
required this.exponent,
required this.canSave,
required this.isEdit,
});
}

View file

@ -6,17 +6,21 @@ class LightmeterTextField extends TextFormField {
super.focusNode,
super.initialValue,
super.inputFormatters,
super.maxLength,
super.onChanged,
super.style,
super.textAlign,
Widget? leading,
String? hintText,
}) : super(
autovalidateMode: AutovalidateMode.onUserInteraction,
maxLines: 1,
decoration: InputDecoration(
counter: const SizedBox(),
contentPadding: EdgeInsets.zero,
errorStyle: const TextStyle(fontSize: 0),
icon: leading,
hintText: hintText,
),
validator: (value) {
if (value == null || value.isEmpty) {