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?

Roadmap.shで学ぶFlutter - InheritedWidget -

Last updated at Posted at 2024-08-04

導入

前々回から始めているFlutter学習シリーズ。
roadmap.shのFlutterの3本目の記事です。
今回はInheritedWidgetについて。

InheritedWidget

  • InheritedWidgetは、ツリー内の情報を効率的に伝播するための基本クラス
    • 要は、Stateの変化があった際に他のWidgetにもその変化を伝えられるという意味と思われる
  • BuildContext.dependOnInheritedWidgetOfExactType
    • 特定の種類のInheritedWidgetのインスタンスをビルドコンテキストから取得するのに利用する
    • InheritedWidgetがこの方法で参照されると、Stateの変化でConsumerが再ビルドされる
      • FrogColor.dart
        class FrogColor extends InheritedWidget {
          const FrogColor({
            super.key,
            required this.color,
            required super.child,
          });
        
          final Color color;
        
          static FrogColor? maybeOf(BuildContext context) {
            return context.dependOnInheritedWidgetOfExactType<FrogColor>();
          }
        
          static FrogColor of(BuildContext context) {
            final FrogColor? result = maybeOf(context);
            assert(result != null, 'No FrogColor found in context');
            return result!;
          }
        
          @override
          bool updateShouldNotify(FrogColor oldWidget) => color != oldWidget.color;
        }
        

Implementing the of and maybeOf methods

  • FrogColorは、InheritedWidgetを継承し、色情報を保持するクラス
    • maybeOfとofという2つの静的メソッドを持ち、BuildContext.dependOnInheritedWidgetOfExactTypeを呼び出す
    • ofメソッドは非nullのインスタンス(result)を返し、InheritedWidgetが見つからない場合はassertを実行する
    • maybeOfメソッドはnullableなインスタンスを返し、見つからない場合はnullを返す
    • ofメソッドの内部でmaybeOfを呼び出す
  • 他のクラスでInheritedWidgetが実装される際はPrivateだが、ofとmaybeOfメソッドはPublicクラスで実装されることがある
    • 例:ThemeはプライベートなInheritedWidgetを構築するStatelessWidgetにと実装され、Theme.ofは、BuildContext.dependOnInheritedWidgetOfExactTypeを使ってプライベートなInheritedWidgetを検索してThemeDataを返却する

Calling the of or maybeOf methods

  • ofまたはmaybeOfメソッドを使用する場合、コンテキストはInheritedWidgetの子孫である必要がある
  • 例: BuilderのコンテキストがFrogColor(InheritedWidget)の子になっている
// continuing from previous example...
class MyPage extends StatelessWidget {
  const MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FrogColor(
        color: Colors.green,
        child: Builder(
          builder: (BuildContext innerContext) {
            return Text(
              'Hello Frog',
              style: TextStyle(color: FrogColor.of(innerContext).color),
            );
          },
        ),
      ),
    );
  }
}

この場合、innerContext(InheritedWidgetの子であるBuilderのコンテキスト)が使用されており、適切に機能する

  • 機能しない例: MyOtherPageのコンテキストがFrogColorウィジェットの親である場合
// continuing from previous example...

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FrogColor(
        color: Colors.green,
        child: Text(
          'Hello Frog',
          style: TextStyle(color: FrogColor.of(context).color),
        ),
      ),
    );
  }
}

この例では、親のコンテキストのcontextが使用されており、assertが発火されます。

終わりに

InheritedWidgetは、Flutterにおける状態管理の基本的な要素であり、ツリー内の情報を効率的に伝播するために重要です。

特に、ofメソッドやmaybeOfメソッドを活用することで、InheritedWidgetのインスタンスを取得し、状態の変化を子ウィジェットに伝えることができることを理解できました。

ofメソッドは、InheritedWidgetのインスタンスを必ず取得するために使われ、該当するInheritedWidgetが存在しない場合はエラーを発生させます。一方、maybeOfメソッドは、該当するInheritedWidgetが存在しない場合にnullを返すため、柔軟なエラーハンドリングが可能です。

これらのメソッドを適切に使い分けることで、InheritedWidgetの利便性と安全性を高めることができるので、今後は意識して使っていこうと思いました。

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?