3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter】サイズに応じてテキストを縮小する

Last updated at Posted at 2021-05-13

通常対応

FittedBoxを使用して対応する。

FittedBox(
  fit: BoxFit.scaleDown,
  child: Text('長い文章')
)

拡張して対応

全てのTextにFittedBoxを書くのは流石にめんどくさいので
FittedBox+Textのclassを作成してそれを呼び出すようにする

###実装

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

class AutoFitText extends StatelessWidget {
  final String? data;
  final double? width;
  final AlignmentGeometry? alignment;
  final TextAlign? textAlign;
  final TextStyle? style;

  const AutoFitText(
    this.data, {
    Key? key,
    this.width,
    this.alignment,
    this.textAlign,
    this.style,
  })  : assert(
          data != null,
          'A non-null String',
        ),
        super(key: key);

  @override
  Widget build(Object context) {
    return Container(
      width: width,
      alignment: alignment,
      child: FittedBox(
        fit: BoxFit.scaleDown,
        child: Text('$data', textAlign: textAlign, style: style),
      ),
    );
  }
}

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?