简体   繁体   English

TextStyle不适用于ListView itemBuilder

[英]TextStyle not applied to ListView itemBuilder

I'm new to Flutter/dart and running through the tutorial. 我是Flutter / dart的新手,正在学习本教程。 I'm trying to set the text style of a ListView but it's not being applied. 我正在尝试设置ListView的文本样式,但未应用。

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _font = TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold, color: Colors.blueAccent);

  Widget _buildSuggestions() {
    return ListView.builder(
      itemBuilder: (context, i) {
        if (i.isOdd) return Divider();
        final index = i ~/ 2;
        if (index >= _suggestions.length) {
          _suggestions.addAll(generateWordPairs().take(10));
        }
        return _buildRow(_suggestions[index]);
      }
    );
  }

  Widget _buildRow(WordPair w) {
    print(_font);
    return ListTile(
      title: Text(
        WordPair.random().asPascalCase,
        style: _font)
      );
  }

  @override
  Widget build(BuildContext context) {
    return _buildSuggestions();
  }

}

It works if I create a new TextStyle directly within the Text Widget, but doesn't when I supply a final ivar provided by the State class itself and I wonder why? 如果我直接在Text Widget中创建一个新的TextStyle它将起作用,但是当我提供State类本身提供的最终ivar时,它不起作用,我想知道为什么吗?

So this works: 所以这有效:

  Widget _buildRow(WordPair w) {
    print(_font);
    return ListTile(
      title: Text(
        WordPair.random().asPascalCase,
        style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold, color: Colors.blueAccent))
      );
  }

But this doesn't: 但这不是:

  Widget _buildRow(WordPair w) {
    print(_font);
    return ListTile(
      title: Text(
        WordPair.random().asPascalCase,
        style: _font)
      );
  }

It's because hot reload recalls your build function in the state class again, but the class is not reloaded again, so it doesn't pick any changes outside of your build function. 这是因为热重载会再次在状态类中调用您的构建函数,但是不会再次重载该类,因此它不会在构建函数之外进行任何更改。 Do a full reload in order to do it. 做一个完整的重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM