我在这个应用程序中使用 freezed with bloc。单击浮动操作按钮时,我需要用户,出现底部工作表。当他的表格有效时,他将再次单击浮动操作按钮,他的数据将保存在数据库中。这是我的代码。但我觉得这段代码没有将 UI 与逻辑层分开。是比这更好的方法吗?
用户界面代码:
floatingActionButton: BlocBuilder<HomeCubit, HomeState>(
builder: (context, state) {
return InkWell(
onTap: () async {
if (state.isOpenBottomSheet) {
context
.read<HomeCubit>()
.addNewNoteButton(isOpenBottomSheet: true);
_scaffoldKey.currentState!
.showBottomSheet(
(context) => const AddNewNoteBottomSheet())
.closed
.then(
(_) => context
.read<HomeCubit>()
.addNewNoteButton(isOpenBottomSheet: true),
);
} else {
if (state.inputTextValue.input.isRight()) {
final DBHelper _dbHelper = DBHelper();
await _dbHelper.insertNodeToDB(
noteDatabaseModel: NoteDatabaseModel(
content: state.inputTextValue.input
.getOrElse(() => 'invalid note'),
status: state.radioValue,
),
);
}
}
},
child: Container(
clipBehavior: Clip.antiAliasWithSaveLayer,
height: 11.h,
width: 10.h,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(Radius.circular(1.w)),
color: const Color(accentColor),
),
child: Icon(
Icons.add,
color: const Color(whiteColor),
size: 9.h,
),
),
);
国家代码:
@freezed
abstract class HomeState with _$HomeState {
const factory HomeState({
required Note inputTextValue,
required bool isOpenBottomSheet,
}) = AddNewNoteClicked;
factory HomeState.init() => HomeState(
inputTextValue: Note(textInput: ''),
isOpenBottomSheet: true,
);
}
Cubit的代码是:
class HomeCubit extends Cubit<HomeState> {
HomeCubit() : super(HomeState.init());
void addNewNoteButton({bool? isOpenBottomSheet}) async {
emit(state.copyWith(isOpenBottomSheet: isOpenBottomSheet ?? true));
}
}