Flutter Dart:常量类还是常量变量?

Flutter Dart:常量类还是常量变量?,flutter,class,dart,var,Flutter,Class,Dart,Var,新问题。 就性能而言,下面的类(用于构建文本字段的小部件)之间存在很大差异, 还是一个不会重建任何东西的常量?我在Flatter中听到过,因为所有东西都是小部件,当构建例如func vs class时,总是构建一个类来替换在代码上构建的任何东西。我想这可能是一样的 class RoundedBox extends StatelessWidget { const RoundedBox({ this.text, this.onChanged, }); final Stri

新问题。 就性能而言,下面的类(用于构建文本字段的小部件)之间存在很大差异, 还是一个不会重建任何东西的常量?我在Flatter中听到过,因为所有东西都是小部件,当构建例如func vs class时,总是构建一个类来替换在代码上构建的任何东西。我想这可能是一样的

class RoundedBox extends StatelessWidget {
  const RoundedBox({
    this.text,
    this.onChanged,
  });
  final String text;
  final Function onChanged;

  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: onChanged,
      decoration: InputDecoration(
        hintText: text,
        hintStyle: TextStyle(
          color: Colors.grey,
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.lightBlueAccent, width: 1.0),
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.lightBlueAccent, width: 2.0),
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
      ),
    );
  }
}
或者只是一个简单的常量变量

const kTextFieldDecoration = InputDecoration(
  hintText: 'Enter a value',
  hintStyle: TextStyle(
    color: Colors.grey,
  ),
  contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
  border: OutlineInputBorder(
    borderRadius: BorderRadius.all(Radius.circular(32.0)),
  ),
  enabledBorder: OutlineInputBorder(
    borderSide: BorderSide(color: Colors.lightBlueAccent, width: 1.0),
    borderRadius: BorderRadius.all(Radius.circular(32.0)),
  ),
  focusedBorder: OutlineInputBorder(
    borderSide: BorderSide(color: Colors.lightBlueAccent, width: 2.0),
    borderRadius: BorderRadius.all(Radius.circular(32.0)),
  ),
);

在这种情况下,如果重用定义为const的
InputDecoration
,您将获得更好的性能

但这是等效的(注意输入装饰前的const)。对于不会改变的
小部件
,最好使用
const


  @override
  Widget build(BuildContext context) {
    return TextField(
      onChanged: onChanged,
      decoration: const InputDecoration(
        hintText: text,
        hintStyle: TextStyle(
          color: Colors.grey,
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.lightBlueAccent, width: 1.0),
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.lightBlueAccent, width: 2.0),
          borderRadius: BorderRadius.all(Radius.circular(32.0)),
        ),
      ),
    );
  }
}

谢谢你@YoBo。我想一切都取决于我们需要知道什么是最好的方法。