美文网首页
InheritedWidget共享数据学习

InheritedWidget共享数据学习

作者: XH小子 | 来源:发表于2022-10-12 16:31 被阅读0次
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ShareDataWidget<T> extends InheritedWidget {
  ShareDataWidget({Key? key, required this.child, required this.data})
      : super(key: key, child: child);

  final Widget child;
  final T data;

  @override
  bool updateShouldNotify(ShareDataWidget<T> oldWidget) {
    return true;
  }
}

class ChangeNotifierProvider<T extends ChangeNotifier> extends StatefulWidget {
  ChangeNotifierProvider({Key? key, required this.child, required this.creater})
      : super(key: key);
  final T Function(BuildContext context) creater;
  final Widget child;
  //添加一个listen参数,表示是否建立依赖关系
  static T of<T>(BuildContext context, {bool listen = true}) {
    final provider = listen
        ? context.dependOnInheritedWidgetOfExactType<ShareDataWidget<T>>()
        : context
            .getElementForInheritedWidgetOfExactType<ShareDataWidget<T>>()
            ?.widget as ShareDataWidget<T>;
    return provider!.data;
  }

  // 需要使用_ChangeNotifierProviderState<T> 不然后会provider会是null
  // @override
  // State<ChangeNotifierProvider> createState() =>
  //     _ChangeNotifierProviderState<T>();
  _ChangeNotifierProviderState<T> createState() =>
      _ChangeNotifierProviderState<T>();
}

class _ChangeNotifierProviderState<T extends ChangeNotifier>
    extends State<ChangeNotifierProvider> {
  late T data;
  @override
  void initState() {
    data = widget.creater(context) as T;
    data.addListener(update);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ShareDataWidget(child: widget.child, data: data);
  }

  @override
  void dispose() {
    data.removeListener(update);
    super.dispose();
  }

  update() {
    setState(() {});
  }
}

class Consumer<T> extends StatelessWidget {
  const Consumer({Key? key, required this.builder}) : super(key: key);
  final Widget Function(BuildContext context, T value) builder;

  @override
  Widget build(BuildContext context) {
    return builder(context, ChangeNotifierProvider.of<T>(context));
  }
}

class ThemeState extends ChangeNotifier {
  late int settingThemeCoror;
  ThemeState({required this.settingThemeCoror});
  void changeTheme(int thmeColor) {
    this.settingThemeCoror = thmeColor;
    notifyListeners();
  }
}

class InheritedWidgetTestRoute extends StatefulWidget {
  @override
  _InheritedWidgetTestRouteState createState() =>
      _InheritedWidgetTestRouteState();
}

class _InheritedWidgetTestRouteState extends State<InheritedWidgetTestRoute> {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeState>(
      creater: (context) => ThemeState(settingThemeCoror: 222),
      child: Consumer<ThemeState>(builder: (context, value) {
        return Column(
          children: [
            Container(
              child: Text(value.settingThemeCoror.toString()),
            ),
            CupertinoButton(
                child: Text("新增"),
                onPressed: () {
                  value.changeTheme(12345);
                })
          ],
        );
      }),
    );
  }
}

相关文章

网友评论

      本文标题:InheritedWidget共享数据学习

      本文链接:https://www.haomeiwen.com/subject/glmfzrtx.html