扑:如何发现对继承的小部件的依赖性?

时间:2019-06-19 12:25:03

标签: flutter dart

我目前正在阅读provider软件包的示例代码:

// ignore_for_file: public_member_api_docs
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() => runApp(MyApp());

class Counter with ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(builder: (_) => Counter()),
      ],
      child: Consumer<Counter>(
        builder: (context, counter, _) {
          return MaterialApp(
            supportedLocales: const [Locale('en')],
            localizationsDelegates: [
              DefaultMaterialLocalizations.delegate,
              DefaultWidgetsLocalizations.delegate,
              _ExampleLocalizationsDelegate(counter.count),
            ],
            home: const MyHomePage(),
          );
        },
      ),
    );
  }
}

class ExampleLocalizations {
  static ExampleLocalizations of(BuildContext context) =>
      Localizations.of<ExampleLocalizations>(context, ExampleLocalizations);

  const ExampleLocalizations(this._count);

  final int _count;

  String get title => 'Tapped $_count times';
}

class _ExampleLocalizationsDelegate
    extends LocalizationsDelegate<ExampleLocalizations> {
  const _ExampleLocalizationsDelegate(this.count);

  final int count;

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'en';

  @override
  Future<ExampleLocalizations> load(Locale locale) =>
      SynchronousFuture(ExampleLocalizations(count));

  @override
  bool shouldReload(_ExampleLocalizationsDelegate old) => old.count != count;
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Title()),
      body: const Center(child: CounterLabel()),
      floatingActionButton: const IncrementCounterButton(),
    );
  }
}

class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: Provider.of<Counter>(context).increment,
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }
}

class CounterLabel extends StatelessWidget {
  const CounterLabel({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final counter = Provider.of<Counter>(context);
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        const Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '${counter.count}',
          style: Theme.of(context).textTheme.display1,
        ),
      ],
    );
  }
}

class Title extends StatelessWidget {
  const Title({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(ExampleLocalizations.of(context).title);
  }
}

当用户在FloatingRadioButton内按下IncrementCounterButton时,将在build()CounterLabel上调用IncrementCounterButton

它们都依赖于继承的窗口小部件,该窗口小部件已更新。 颤动如何发现这种依赖性?

我假设BuildContext是通过调用Provider.of<>()来修改的。 这就是为什么我们添加IncrementCounterButton本身没有功能的原因吗? 只是将调用移到更大父控件之外的Provider.of<>()上,这将使重建成本更高?

1 个答案:

答案 0 :(得分:1)

绑定小部件InheritedWidget及其使用者是通过BuildContext创建的。

考虑以下InheritedWidget:

class Foo extends InheritedWidget {}

然后Foo的后代可以通过调用以下内容来订阅它:

BuildContext context
context.inheritFromWidgetOfExactType(Foo);

值得注意的是,小部件可以通过执行以下操作而无需订阅即可获取InheritedWidget

BuildContext context
context.ancestorInheritedElementForWidgetOfExactType(Foo);

此调用通常由.of(context)模式在内部执行。

对于provider,该订阅是通过调用Provider.of<T>(context)完成的。

provider还公开了一个可选参数,以有目的地 not 订阅继承的小部件:

T value = Provider.of<T>(context, listen: false);