getPitchWidget method

  1. @override
Widget getPitchWidget(
  1. {required AppModel appModel,
  2. required BuildContext context,
  3. required String reading,
  4. required int downstep}
)
override

Some languages may have custom widgets for generating pronunciation diagrams.

Implementation

@override
Widget getPitchWidget({
  required AppModel appModel,
  required BuildContext context,
  required String reading,
  required int downstep,
}) {
  List<Widget> listWidgets = [];

  Color color = Theme.of(context).brightness == Brightness.dark
      ? Colors.white
      : Colors.black;

  Widget getAccentTop(String text) {
    return Container(
      padding: const EdgeInsets.only(top: 1),
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(color: color),
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: color,
          fontSize: appModel.dictionaryFontSize,
        ),
      ),
    );
  }

  Widget getAccentEnd(String text) {
    return Container(
      padding: const EdgeInsets.only(top: 1),
      decoration: BoxDecoration(
        border: Border(
          top: BorderSide(color: color),
          right: BorderSide(color: color),
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: color,
          fontSize: appModel.dictionaryFontSize,
        ),
      ),
    );
  }

  Widget getAccentNone(String text) {
    return Container(
      padding: const EdgeInsets.only(top: 1),
      decoration: const BoxDecoration(
        border: Border(
          top: BorderSide(color: Colors.transparent),
        ),
      ),
      child: Text(
        text,
        style: TextStyle(
          color: color,
          fontSize: appModel.dictionaryFontSize,
        ),
      ),
    );
  }

  List<String> moras = [];
  for (int i = 0; i < reading.length; i++) {
    String current = reading[i];
    String? next;
    if (i + 1 < reading.length) {
      next = reading[i + 1];
    }

    if (next != null && 'ゃゅょぁぃぅぇぉャュョァィゥェォ'.contains(next)) {
      moras.add(current + next);
      i += 1;
      continue;
    } else {
      moras.add(current);
    }
  }

  if (downstep == 0) {
    for (int i = 0; i < moras.length; i++) {
      if (i == 0) {
        listWidgets.add(getAccentNone(moras[i]));
      } else {
        listWidgets.add(getAccentTop(moras[i]));
      }
    }
  } else {
    for (int i = 0; i < moras.length; i++) {
      if (i == 0 && i != downstep - 1) {
        listWidgets.add(getAccentNone(moras[i]));
      } else if (i < downstep - 1) {
        listWidgets.add(getAccentTop(moras[i]));
      } else if (i == downstep - 1) {
        listWidgets.add(getAccentEnd(moras[i]));
      } else {
        listWidgets.add(getAccentNone(moras[i]));
      }
    }
  }

  listWidgets.add(
    Text(
      ' [$downstep]  ',
      style: TextStyle(
        color: color,
        fontSize: appModel.dictionaryFontSize,
      ),
    ),
  );

  Widget widget = Wrap(
    crossAxisAlignment: WrapCrossAlignment.end,
    children: listWidgets,
  );

  return widget;
}