gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:18:08 24 4
gpt4 key购买 nike

我目前正在阅读 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);
}
}

当用户按下 FloatingRadioButtonIncrementCounterButton 内, build()被称为 CounterLabelIncrementCounterButton .

它们都依赖于一个更新的继承小部件。Flutter 是如何发现这种依赖的?

我假设 BuildContext通过调用 Provider.of<>() 进行修改.这就是我们添加 IncrementCounterButton 的原因吗? ,它本身没有功能?只是将调用转移到 Provider.of<>()在其更大的父小部件之外,重建哪个会更昂贵?

最佳答案

绑定(bind)小部件 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还公开了一个可选参数以有目的地订阅继承的小部件:

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

关于 flutter :如何发现对继承的小部件的依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56667584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com