tarkalabs / tarka-ui-kit-flutter

UI Kit and Design System for building apps with Flutter
https://pub.dev/packages/tarka_ui
MIT License
7 stars 0 forks source link

Refactor TUIMenuItem #71

Open kalpeshp0310 opened 4 months ago

kalpeshp0310 commented 4 months ago
  1. Why abstract TUIMenuItemProperties as a separate class when it is only going to be used in TUIMenuItem as an argument. Instead, we can just take all properties of TUIMenuItemProperties inside the TUIMenuItem constructor directly. This will make the calling side simple as they do not have to create an instance of TUIMenuItemProperties every time they create an instance of TUIMenuItem

Change this.

class TUIMenuItemProperties {
  late String title;
  late TUIMenuItemStyle style;
  late TUIMenuItemState state;

  TUIMenuItemProperties({
    required this.title,
    required this.style,
    this.state = TUIMenuItemState.unchecked,
  });
}

class TUIMenuItem extends StatefulWidget {
  final TUIMenuItemProperties item;
  final bool backgroundDark; // for hover
  final Function(TUIMenuItemState)? action;
  final Function(TUIMenuItemState)? onLeftTap;
  final Function(TUIMenuItemState)? onRightTap;

  const TUIMenuItem({
    super.key,
    required this.item,
    this.backgroundDark = false,
    this.onLeftTap,
    this.onRightTap,
    this.action,
  });

 ...
}

To this.

class TUIMenuItem extends StatefulWidget {
  final String title;
  final TUIMenuItemStyle style;
  final TUIMenuItemState state;
  final bool backgroundDark; // for hover
  final Function(TUIMenuItemState)? action;
  final Function(TUIMenuItemState)? onLeftTap;
  final Function(TUIMenuItemState)? onRightTap;

  const TUIMenuItem({
    super.key,
    required this.title,
    required this.style,
    this.state = TUIMenuItemState.unchecked,
    this.backgroundDark = false,
    this.onLeftTap,
    this.onRightTap,
    this.action,
  });

 ...
}
  1. Improve enum value names. The names are not meaningful.
enum TUIMenuItemStyle {
  none(0),
  onlyLeft(1),
  onlyRight(2),
  both(3);

  const TUIMenuItemStyle(this.value);

  final num value;

  static TUIMenuItemStyle getByValue(num i) {
    return TUIMenuItemStyle.values.firstWhere((x) => x.value == i);
  }
}