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

When font size is large, tip is not show completely #771

Open
calcitem opened this issue Feb 13, 2024 · 1 comment
Open

When font size is large, tip is not show completely #771

calcitem opened this issue Feb 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@calcitem
Copy link
Owner

When font size is large, tip is not show completely

@calcitem calcitem added the bug Something isn't working label Feb 13, 2024
@calcitem
Copy link
Owner Author

Doesn't work:

import 'package:flutter/material.dart';
import 'package:marquee/marquee.dart';

class HeaderTip extends StatefulWidget {
  @override
  HeaderTipState createState() => HeaderTipState();
}

class HeaderTipState extends State<HeaderTip> {
  final ValueNotifier<String> _messageNotifier = ValueNotifier<String>("");

  @override
  void initState() {
    super.initState();
    GameController().headerTipNotifier.addListener(_showTip);
  }

  void _showTip() {
    final HeaderTipNotifier headerTipNotifier =
        GameController().headerTipNotifier;

    if (headerTipNotifier.showSnackBar) {
      rootScaffoldMessengerKey.currentState!
          .showSnackBarClear(headerTipNotifier.message);
    }

    _messageNotifier.value = headerTipNotifier.message;
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<String>(
      valueListenable: _messageNotifier,
      builder: (BuildContext context, String value, Widget? child) {
        final textStyle = TextStyle(
          color: DB().colorSettings.messageColor,
          fontSize: AppTheme.textScaler.scale(AppTheme.defaultFontSize),
          fontFeatures: const <FontFeature>[FontFeature.tabularFigures()],
        );

        return Semantics(
          enabled: true,
          child: SizedBox(
            height: 24 * DB().displaySettings.fontScale,
            child: LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {
                final textWidth = _getTextWidth(value, textStyle, context);
                final isOverflowing = textWidth > constraints.maxWidth;

                return isOverflowing
                    ? Marquee(
                        text: value.isEmpty ? S.of(context).welcome : value,
                        style: textStyle,
                        scrollAxis: Axis.horizontal,
                        blankSpace: 20.0,
                        velocity: 50.0,
                        pauseAfterRound: Duration(seconds: 1),
                        startPadding: 10.0,
                        accelerationDuration: Duration(seconds: 1),
                        accelerationCurve: Curves.linear,
                        decelerationDuration: Duration(milliseconds: 500),
                        decelerationCurve: Curves.easeOut,
                      )
                    : Text(
                        value.isEmpty ? S.of(context).welcome : value,
                        maxLines: 1,
                        style: textStyle,
                      );
              },
            ),
          ),
        );
      },
    );
  }

  double _getTextWidth(String text, TextStyle style, BuildContext context) {
    final textPainter = TextPainter(
      text: TextSpan(text: text, style: style),
      maxLines: 1,
      textDirection: TextDirection.ltr,
    );
    textPainter.layout();
    return textPainter.size.width;
  }

  @override
  void dispose() {
    GameController().headerTipNotifier.removeListener(_showTip);
    super.dispose();
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant