Web应用程序通常反映当前显示的tab's title个内容.

title parameter提供给MaterialAppWidgetsApp意味着我不能将其更改为without rebuilding the entire tree,即在返回MaterialApp的顶层调用setState.

MaterialApp(
  title: 'Web app title',
  ...,
)

我希望能够在any time的应用程序中将app titleanywhere更改为app title.我该怎么做?

推荐答案

Flutter 中的动态应用程序标题

app title可以使用SystemChrome.setApplicationSwitcherDescription:

@override
Widget build(BuildContext context) {
    SystemChrome.setApplicationSwitcherDescription(ApplicationSwitcherDescription(
      标签: 'Dynamic web app title',
      原色: Theme.of(context).原色.value,
    ));


  return Container(...);
}

Screen capture

SystemChromeimport 'package:flutter/services.dart';一起使用,你想在103 method中拨打setApplicationSwitcherDescription.

Title也是这样做的(参见source code),这是WidgetsAppMaterialApp等使用的小部件.

因此,您也可以使用Title小部件,而不是自己访问SystemChrome:

@override
Widget build(Context context) {
  return Title(
    标签: 'Dynamic app title',
    原色: Theme.of(context).原色,
    child: ..,
  );
}

标签

标签参数非常简单:它精确地映射到MaterialApp中的titleString.

原色

The 原色 will determine the color the system might use in the application switcher, e.g. the title bar color in recents on Android.
This is an int instead of a Color, which is why you need to call Color.value to convert your primary color to an integer.

Interference with other Titles

You might ask yourself if calling setApplicationSwitcherDescription or inserting a Title widget when the title property on MaterialApp is set can cause any problems.
The answer is: no because the widget that is the deepest down in the tree will set the title. Essentially, even if the whole app rebuilds, your setApplicationSwitcherDescription will be called after that of MaterialApp assuming you are calling it in a child and therefore you will override the MaterialApp title.

Dart相关问答推荐

dart中如何从xpath获取属性值?

在 Flutter 中从树中临 timeshift 除时保留小部件状态

如何在polymer应用程序中实现主要功能

在 Flutter 中检测键盘事件

如何从 Flutter App 连接 Ms SQL?

Bad state:在Flutter中从 addStream 添加项目时,您无法关闭主题

如何在 Dart 中设置文本框的值?

如何在 Flutter/dart 中获取一周中特定日期的日期?

FInal和top-level lazy初始化

为什么使用Dart作为前端开发人员?

如何在 Dart 中使用类型Aliases/Typedefs(也是非函数)?

Flutter中图像纵横比的变化

向 javascript 公开 Dart 函数

如何使用工厂构造函数扩展抽象类?

如何循环遍历元素列表

Dart 构造函数与静态方法;例如,为什么 int.parse() 不是工厂构造函数?

带范围的dartswitch

如何在 Dart 中扁平化map列表?

在 Dart 和 Pub 中,我应该将 pubspec.lock 添加到我的 .gitignore 吗?

你如何在 Dart 中构建一个单例?