Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autosize to name styling #1254

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 71 additions & 46 deletions lib/shared/full_name_widgets.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:thunder/core/enums/font_scale.dart';
Expand All @@ -16,6 +17,7 @@ class UserFullNameWidget extends StatelessWidget {
final TextStyle? textStyle;
final bool includeInstance;
final FontScale? fontScale;
final bool autoSize;

const UserFullNameWidget(
this.outerContext,
Expand All @@ -30,6 +32,7 @@ class UserFullNameWidget extends StatelessWidget {
this.textStyle,
this.includeInstance = true,
this.fontScale,
this.autoSize = false,
}) : assert(outerContext != null || (userSeparator != null && userNameThickness != null && userNameColor != null && instanceNameThickness != null && instanceNameColor != null)),
assert(outerContext != null || textStyle != null);

Expand All @@ -43,35 +46,45 @@ class UserFullNameWidget extends StatelessWidget {
NameColor instanceNameColor = this.instanceNameColor ?? outerContext!.read<ThunderBloc>().state.userFullNameInstanceNameColor;
TextStyle? textStyle = this.textStyle ?? Theme.of(outerContext!).textTheme.bodyMedium;

return Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
TextSpan(
children: [
TextSpan textSpan = TextSpan(
children: [
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: userNameThickness.toWeight(),
color: userNameColor.color == NameColor.defaultColor ? null : userNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: userNameThickness.toWeight(),
color: userNameColor.color == NameColor.defaultColor ? null : userNameColor.toColor(context),
text: suffix,
style: textStyle.copyWith(
fontWeight: instanceNameThickness.toWeight(),
color: instanceNameColor.color == NameColor.defaultColor ? null : instanceNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: suffix,
style: textStyle.copyWith(
fontWeight: instanceNameThickness.toWeight(),
color: instanceNameColor.color == NameColor.defaultColor ? null : instanceNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
],
),
],
);

return autoSize
? AutoSizeText.rich(
softWrap: false,
maxLines: 1,
overflow: TextOverflow.fade,
style: textStyle,
textSpan,
)
: Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
textSpan,
);
}
}

Expand All @@ -87,6 +100,7 @@ class CommunityFullNameWidget extends StatelessWidget {
final TextStyle? textStyle;
final bool includeInstance;
final FontScale? fontScale;
final bool autoSize;

const CommunityFullNameWidget(
this.outerContext,
Expand All @@ -101,6 +115,7 @@ class CommunityFullNameWidget extends StatelessWidget {
this.textStyle,
this.includeInstance = true,
this.fontScale,
this.autoSize = false,
}) : assert(outerContext != null || (communitySeparator != null && communityNameThickness != null && communityNameColor != null && instanceNameThickness != null && instanceNameColor != null)),
assert(outerContext != null || textStyle != null);

Expand All @@ -114,34 +129,44 @@ class CommunityFullNameWidget extends StatelessWidget {
NameColor instanceNameColor = this.instanceNameColor ?? outerContext!.read<ThunderBloc>().state.communityFullNameInstanceNameColor;
TextStyle? textStyle = this.textStyle ?? Theme.of(outerContext!).textTheme.bodyMedium;

return Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
TextSpan(
children: [
TextSpan textSpan = TextSpan(
children: [
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: communityNameThickness.toWeight(),
color: communityNameColor.color == NameColor.defaultColor ? null : communityNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: prefix,
style: textStyle!.copyWith(
fontWeight: communityNameThickness.toWeight(),
color: communityNameColor.color == NameColor.defaultColor ? null : communityNameColor.toColor(context),
text: suffix,
style: textStyle.copyWith(
fontWeight: instanceNameThickness.toWeight(),
color: instanceNameColor.color == NameColor.defaultColor ? null : instanceNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
if (includeInstance == true)
TextSpan(
text: suffix,
style: textStyle.copyWith(
fontWeight: instanceNameThickness.toWeight(),
color: instanceNameColor.color == NameColor.defaultColor ? null : instanceNameColor.toColor(context),
fontSize:
outerContext == null ? null : MediaQuery.textScalerOf(context).scale((textStyle.fontSize ?? textStyle.fontSize!) * (fontScale?.textScaleFactor ?? FontScale.base.textScaleFactor)),
),
),
],
),
],
);

return autoSize
? AutoSizeText.rich(
softWrap: false,
maxLines: 1,
overflow: TextOverflow.fade,
style: textStyle,
textSpan,
)
: Text.rich(
softWrap: false,
overflow: TextOverflow.fade,
style: textStyle,
textScaler: TextScaler.noScaling,
textSpan,
);
}
}
15 changes: 6 additions & 9 deletions lib/user/widgets/user_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:auto_size_text/auto_size_text.dart';

import 'package:thunder/core/enums/full_name.dart';
import 'package:thunder/shared/avatars/user_avatar.dart';
import 'package:thunder/shared/full_name_widgets.dart';
import 'package:thunder/shared/icon_text.dart';
import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/numbers.dart';
Expand Down Expand Up @@ -107,14 +107,11 @@ class _UserHeaderState extends State<UserHeader> {
style: theme.textTheme.headlineSmall?.copyWith(fontWeight: FontWeight.w600),
maxLines: 1,
),
AutoSizeText(
generateUserFullName(
context,
widget.getPersonDetailsResponse.personView.person.name,
fetchInstanceNameFromUrl(widget.getPersonDetailsResponse.personView.person.actorId) ?? 'N/A',
),
style: theme.textTheme.bodyMedium,
maxLines: 1,
UserFullNameWidget(
context,
widget.getPersonDetailsResponse.personView.person.name,
fetchInstanceNameFromUrl(widget.getPersonDetailsResponse.personView.person.actorId),
autoSize: true,
),
const SizedBox(height: 8.0),
Row(
Expand Down
Loading