0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Flutter]Visibilityで非表示にしつつWidgetはビルドする方法

Posted at

maintainStateを活用することで状態を保持する=Widgetのビルド自体は実行する。
Widgetのビルドに時間がかかり、あらかじめビルドしておきたい場合に有効。

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

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Visibility(
          // 状態は保持する
          maintainState: true,
          // 見た目は非表示
          visible: false,
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    // ビルドされたらprintする
    print("build.start");
    return const Placeholder();
  }
}

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?