1
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] AppBarだけをConsumerにしたい

Posted at

課題

RiverpodのConsumerはWidgetのため、ScaffoldのAppBarには適用できない

解決方針

Consumerをカスタマイズして、PreferredSizeWidgetに準拠させる

実装

typedef PreferredConsumerBuilder = PreferredSizeWidget Function(
  BuildContext context,
  WidgetRef ref,
  Widget? child,
);

@sealed
class AppBarConsumer extends ConsumerWidget implements PreferredSizeWidget {
  const AppBarConsumer({
    super.key,
    required PreferredConsumerBuilder builder,
    Size preferredSize = const Size.fromHeight(48),
    Widget? child,
  })  : _child = child,
        _builder = builder,
        _preferredSize = preferredSize;

  final PreferredConsumerBuilder _builder;
  final Widget? _child;
  final Size _preferredSize;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return _builder(context, ref, _child);
  }

  @override
  Size get preferredSize => _preferredSize;
}

懸念と対応

preferredSizeがbuilderで生成するAppBarのpreferredSizeと一致しない可能性があるので、そこはbuilderで生成するWidgtの型をabstractなどを使用してさらに制限する方法もあるかなと

1
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
1
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?