[Flutter] 스타일의 copyWith는 좋지만 테마라면 조심해야 할 번거로운 이야기

5857 단어 Flutter
안녕하세요, Flutter 아기입니다.

이번은 매우 쓸데없는 실험을, 시코타마 백열한 열기에 싸인 PC 팬의 소리를 들으면서, 시끄럽게 해냈습니다. 생각하면 당연한 것을 실험하는 것은, 과연 낭비인가 그렇지 않은지, 여러분의 의견에 맡깁니다.

일의 발단



테마를 쓰면 좋다고는 잘 알려져 있지만, 쓰는 법이 금방 잘 모르겠다. copyWith로 원래 있는 테마를 사용해 돌리는 것은 알지만, 무엇인가 겨우 여러가지 조사하는 것도, 스스로 시험하는 것이 빠르지 않을까라고 하는 생각에 사로잡혀 버렸습니다.

시도한 것



우선 하나의 요소만 적용한 스타일 3개를 설정하고, 이것들을 copyWith와 merge로 합성했습니다. 마지막으로, 테마로서 copyWith로 합성해 보았습니다.
// 単体
final large = TextStyle(fontSize: 30);
final red = TextStyle(color: Colors.red);
final line = TextStyle(decoration: TextDecoration.lineThrough);

// スタイル合成
final copy = large
    .copyWith(color: Colors.red)
    .copyWith(decoration: TextDecoration.lineThrough);
final merge = large.merge(red).merge(line);

// テーマ合成
final copyTheme = ThemeData(textTheme: TextTheme(headline1: large))
    .copyWith(textTheme: TextTheme(headline1: red))
    .copyWith(textTheme: TextTheme(headline1: line));

이들은 다음 코드로 표시됩니다.
Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    Text("--- 単体 ---"),
    Text("large", style: large),
    Text("red", style: red),
    Text("line", style: line),
    Text("\n--- スタイル合成 ---"),
    Text("copy", style: copy),
    Text("merge", style: merge),
    Text("\n--- テーマ合成 ---"),
    Text("copy theme", style: copyTheme.textTheme.headline1),
  ],
)

결과





스타일만을 copyWith나 merge한 것은 모두 잘 합성되어 있습니다. 한편 테마를 copyWith한 것은 마지막 스타일만 적용되고 있네요.

이상으로부터, 진심으로 테마를 합성하려고 하면, 스타일 하나 하나에 대해서 copyWith나 merge를 걸지 않으면 안 되는 것 같습니다. 실로 귀찮은 사실을 깨달아 버린 것입니다만, 겨우가 해소되었으므로 좋았던 일로 해 둡니다.

이상입니다. 여름이 끝에 다가오고 있습니다 만, 미안해.

좋은 웹페이지 즐겨찾기