feat: use stateless widget

This commit is contained in:
Vincent Chan 2022-08-30 19:19:52 +08:00
parent 27557c52a3
commit 1718d03884
5 changed files with 34 additions and 109 deletions

View File

@ -24,7 +24,7 @@ typedef SwitchToFieldCallback
FieldType fieldType, FieldType fieldType,
); );
class FieldTypeOptionEditor extends StatefulWidget { class FieldTypeOptionEditor extends StatelessWidget {
final TypeOptionDataController dataController; final TypeOptionDataController dataController;
final PopoverMutex popoverMutex; final PopoverMutex popoverMutex;
@ -34,29 +34,15 @@ class FieldTypeOptionEditor extends StatefulWidget {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@override
State<FieldTypeOptionEditor> createState() => _FieldTypeOptionEditorState();
}
class _FieldTypeOptionEditorState extends State<FieldTypeOptionEditor> {
late PopoverController popover;
String? currentOverlayIdentifier;
@override
void initState() {
popover = PopoverController(mutex: widget.popoverMutex);
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (context) => FieldTypeOptionEditBloc(widget.dataController) create: (context) => FieldTypeOptionEditBloc(dataController)
..add(const FieldTypeOptionEditEvent.initial()), ..add(const FieldTypeOptionEditEvent.initial()),
child: BlocBuilder<FieldTypeOptionEditBloc, FieldTypeOptionEditState>( child: BlocBuilder<FieldTypeOptionEditBloc, FieldTypeOptionEditState>(
builder: (context, state) { builder: (context, state) {
List<Widget> children = [ List<Widget> children = [
_switchFieldTypeButton(context, widget.dataController.field) _switchFieldTypeButton(context, dataController.field)
]; ];
final typeOptionWidget = final typeOptionWidget =
_typeOptionWidget(context: context, state: state); _typeOptionWidget(context: context, state: state);
@ -79,13 +65,15 @@ class _FieldTypeOptionEditorState extends State<FieldTypeOptionEditor> {
return SizedBox( return SizedBox(
height: GridSize.typeOptionItemHeight, height: GridSize.typeOptionItemHeight,
child: Popover( child: Popover(
controller: popover, triggerActions:
PopoverTriggerActionFlags.hover | PopoverTriggerActionFlags.click,
mutex: popoverMutex,
offset: const Offset(20, 0), offset: const Offset(20, 0),
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
popupBuilder: (context) { popupBuilder: (context) {
final list = FieldTypeList(onSelectField: (newFieldType) { final list = FieldTypeList(onSelectField: (newFieldType) {
widget.dataController.switchToField(newFieldType); dataController.switchToField(newFieldType);
}); });
return OverlayContainer( return OverlayContainer(
constraints: BoxConstraints.loose(const Size(460, 440)), constraints: BoxConstraints.loose(const Size(460, 440)),
@ -96,11 +84,6 @@ class _FieldTypeOptionEditorState extends State<FieldTypeOptionEditor> {
text: FlowyText.medium(field.fieldType.title(), fontSize: 12), text: FlowyText.medium(field.fieldType.title(), fontSize: 12),
margin: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), margin: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
hoverColor: theme.hover, hoverColor: theme.hover,
onHover: (bool hover) {
if (hover) {
popover.show();
}
},
leftIcon: leftIcon:
svgWidget(field.fieldType.iconName(), color: theme.iconColor), svgWidget(field.fieldType.iconName(), color: theme.iconColor),
rightIcon: svgWidget("grid/more", color: theme.iconColor), rightIcon: svgWidget("grid/more", color: theme.iconColor),
@ -121,8 +104,8 @@ class _FieldTypeOptionEditorState extends State<FieldTypeOptionEditor> {
return makeTypeOptionWidget( return makeTypeOptionWidget(
context: context, context: context,
overlayDelegate: overlayDelegate, overlayDelegate: overlayDelegate,
dataController: widget.dataController, dataController: dataController,
popoverMutex: widget.popoverMutex, popoverMutex: popoverMutex,
); );
} }
@ -141,12 +124,10 @@ class _FieldTypeOptionEditorState extends State<FieldTypeOptionEditor> {
} }
void _hideOverlay(BuildContext context) { void _hideOverlay(BuildContext context) {
if (currentOverlayIdentifier != null) { //
FlowyOverlay.of(context).remove(currentOverlayIdentifier!);
}
} }
} }
abstract class TypeOptionWidget extends StatefulWidget { abstract class TypeOptionWidget extends StatelessWidget {
const TypeOptionWidget({Key? key}) : super(key: key); const TypeOptionWidget({Key? key}) : super(key: key);
} }

View File

@ -42,29 +42,14 @@ class DateTypeOptionWidget extends TypeOptionWidget {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@override
State<StatefulWidget> createState() => _DateTypeOptionWidgetState();
}
class _DateTypeOptionWidgetState extends State<DateTypeOptionWidget> {
late PopoverController dateFormatPopover;
late PopoverController timeFormatPopover;
@override
void initState() {
dateFormatPopover = PopoverController(mutex: widget.popoverMutex);
timeFormatPopover = PopoverController(mutex: widget.popoverMutex);
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (context) => create: (context) =>
DateTypeOptionBloc(typeOptionContext: widget.typeOptionContext), DateTypeOptionBloc(typeOptionContext: typeOptionContext),
child: BlocConsumer<DateTypeOptionBloc, DateTypeOptionState>( child: BlocConsumer<DateTypeOptionBloc, DateTypeOptionState>(
listener: (context, state) => listener: (context, state) =>
widget.typeOptionContext.typeOption = state.typeOption, typeOptionContext.typeOption = state.typeOption,
builder: (context, state) { builder: (context, state) {
return Column(children: [ return Column(children: [
_renderDateFormatButton(context, state.typeOption.dateFormat), _renderDateFormatButton(context, state.typeOption.dateFormat),
@ -78,14 +63,10 @@ class _DateTypeOptionWidgetState extends State<DateTypeOptionWidget> {
Widget _renderDateFormatButton(BuildContext context, DateFormat dataFormat) { Widget _renderDateFormatButton(BuildContext context, DateFormat dataFormat) {
return Popover( return Popover(
controller: dateFormatPopover, mutex: popoverMutex,
child: DateFormatButton(onTap: () { triggerActions:
dateFormatPopover.show(); PopoverTriggerActionFlags.hover | PopoverTriggerActionFlags.click,
}, onHover: ((hover) { child: const DateFormatButton(),
if (hover) {
dateFormatPopover.show();
}
})),
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
offset: const Offset(20, 0), offset: const Offset(20, 0),
@ -107,18 +88,10 @@ class _DateTypeOptionWidgetState extends State<DateTypeOptionWidget> {
Widget _renderTimeFormatButton(BuildContext context, TimeFormat timeFormat) { Widget _renderTimeFormatButton(BuildContext context, TimeFormat timeFormat) {
return Popover( return Popover(
controller: timeFormatPopover, mutex: popoverMutex,
child: TimeFormatButton( triggerActions:
timeFormat: timeFormat, PopoverTriggerActionFlags.hover | PopoverTriggerActionFlags.click,
onTap: () { child: TimeFormatButton(timeFormat: timeFormat),
timeFormatPopover.show();
},
onHover: (hover) {
if (hover) {
timeFormatPopover.show();
}
},
),
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
offset: const Offset(20, 0), offset: const Offset(20, 0),

View File

@ -35,19 +35,13 @@ class MultiSelectTypeOptionWidget extends TypeOptionWidget {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@override
State<StatefulWidget> createState() => _MultiSelectTypeOptionWidgetState();
}
class _MultiSelectTypeOptionWidgetState
extends State<MultiSelectTypeOptionWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SelectOptionTypeOptionWidget( return SelectOptionTypeOptionWidget(
options: widget.selectOptionAction.typeOption.options, options: selectOptionAction.typeOption.options,
beginEdit: () => widget.overlayDelegate.hideOverlay(context), beginEdit: () => overlayDelegate.hideOverlay(context),
overlayDelegate: widget.overlayDelegate, overlayDelegate: overlayDelegate,
typeOptionAction: widget.selectOptionAction, typeOptionAction: selectOptionAction,
// key: ValueKey(state.typeOption.hashCode), // key: ValueKey(state.typeOption.hashCode),
); );
} }

View File

@ -47,33 +47,22 @@ class NumberTypeOptionWidget extends TypeOptionWidget {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@override
State<StatefulWidget> createState() => _NumberTypeOptionWidgetState();
}
class _NumberTypeOptionWidgetState extends State<NumberTypeOptionWidget> {
late PopoverController controller;
@override
void initState() {
controller = PopoverController(mutex: widget.popoverMutex);
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = context.watch<AppTheme>(); final theme = context.watch<AppTheme>();
return BlocProvider( return BlocProvider(
create: (context) => create: (context) =>
NumberTypeOptionBloc(typeOptionContext: widget.typeOptionContext), NumberTypeOptionBloc(typeOptionContext: typeOptionContext),
child: SizedBox( child: SizedBox(
height: GridSize.typeOptionItemHeight, height: GridSize.typeOptionItemHeight,
child: BlocConsumer<NumberTypeOptionBloc, NumberTypeOptionState>( child: BlocConsumer<NumberTypeOptionBloc, NumberTypeOptionState>(
listener: (context, state) => listener: (context, state) =>
widget.typeOptionContext.typeOption = state.typeOption, typeOptionContext.typeOption = state.typeOption,
builder: (context, state) { builder: (context, state) {
return Popover( return Popover(
controller: controller, mutex: popoverMutex,
triggerActions: PopoverTriggerActionFlags.hover |
PopoverTriggerActionFlags.click,
targetAnchor: Alignment.topRight, targetAnchor: Alignment.topRight,
followerAnchor: Alignment.topLeft, followerAnchor: Alignment.topLeft,
offset: const Offset(20, 0), offset: const Offset(20, 0),
@ -91,12 +80,6 @@ class _NumberTypeOptionWidgetState extends State<NumberTypeOptionWidget> {
fontSize: 12), fontSize: 12),
], ],
), ),
onTap: () => controller.show(),
onHover: (hover) {
if (hover) {
controller.show();
}
},
), ),
popupBuilder: (BuildContext context) { popupBuilder: (BuildContext context) {
return OverlayContainer( return OverlayContainer(

View File

@ -34,19 +34,13 @@ class SingleSelectTypeOptionWidget extends TypeOptionWidget {
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@override
State<StatefulWidget> createState() => _SingleSelectTypeOptionWidgetState();
}
class _SingleSelectTypeOptionWidgetState
extends State<SingleSelectTypeOptionWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SelectOptionTypeOptionWidget( return SelectOptionTypeOptionWidget(
options: widget.selectOptionAction.typeOption.options, options: selectOptionAction.typeOption.options,
beginEdit: () => widget.overlayDelegate.hideOverlay(context), beginEdit: () => overlayDelegate.hideOverlay(context),
overlayDelegate: widget.overlayDelegate, overlayDelegate: overlayDelegate,
typeOptionAction: widget.selectOptionAction, typeOptionAction: selectOptionAction,
// key: ValueKey(state.typeOption.hashCode), // key: ValueKey(state.typeOption.hashCode),
); );
} }