无法在标题文本小部件上应用 AppBar titleTextStyle

kos*_*aku 2 appbar dart flutter

我无法使用 AppBar titleTextStyle 更改我的 AppBar 标题文本颜色。我知道我可以通过某些方式设置 AppBar 标题样式,例如在 textWidget 中使用样式或在 AppBar 中设置 textTheme,但我只想知道为什么不能通过设置 titleTextStyle 来更改它。

代码如下。尽管设置了 titleTextStyle 和 foregroundColor,AppBar 标题仍然是白色的。

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        foregroundColor: Colors.black,
        titleTextStyle: TextStyle(color: Colors.black),
        title: const Text('AppBar Color'),),
      body: const Center(
        child: Text('sample'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

DJa*_*ari 5

我知道这个问题是 1 个月前的,但这个问题的答案是:

使用backwardsCompatibility: false,AppBarTheme

appBarTheme: AppBarTheme(
  backwardsCompatibility: false,
  titleTextStyle: TextStyle(
    color: Colors.red,
  ),
),
Run Code Online (Sandbox Code Playgroud)

或另一种方式:

appBarTheme: AppBarTheme(
  textTheme: TextTheme(
    headline6: TextStyle(
      color: Colors.red,
    )
  )
),
Run Code Online (Sandbox Code Playgroud)


Adi*_*haz 1

我认为你需要删除标题的 const

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        foregroundColor: Colors.black,
        titleTextStyle: TextStyle(color: Colors.black),
        title: Text('AppBar Color'),
      ),
      body: const Center(
        child: Text('sample'),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)