Flutter

[flutter] text widget 에서 글씨가 짤리는 경우 해결방법 _ max length

godofwisdom 2022. 4. 8. 14:57
이슈 모습

이미지가 짤리는 Text Widget

 

Flutter 에서 TextField의 경우에는 maxLength 라는옵션이 있어서 바로 적용할수 있었는데,

TextField의 경우에는 
TextField(
  maxLength: 5
),

 

Text 위젝의 경우에는 옵션이 없어 아래와 같은 로직을 추가하여서 적용을 할 수 있었다.

Text 위젯의 경우에는 아래와 같게 한다.
Text(
    _name.length > 15 ? _name.substring(0, 15)+'...' : _name,
),

 

 

google font util
Widget gFontWithStyle({required String txt, required TextStyle stylel}) {
  return Text(
    txt,
    style: GoogleFonts.roboto(
      textStyle: stylel,
    ),
  );
}
replace util
class Utils {
  static String replaceMaxLen( String txt , {int? limit , String? replace}){
    limit = limit ?? 10;
    replace = replace ?? "...";
    return txt.length > limit ? txt.substring(0, limit)+ replace : txt;
  }
}
사용예시
gFontWithStyle(
  txt: Utils.replaceMaxLen(model.descript),
  stylel:
      Theme.of(context).textTheme.titleMedium!.copyWith(
            color: colorMain,
          ),
),

 

 

수정모습