flutter - 保持 Flutter 的样式独立于 View 代码

标签 flutter flutter-layout flutter-dependencies

所以这就是我今天设计一个简单的 flutter 按钮的方式 -

FlatButton(
          child: Text('Hello'),
          onPressed: () {},
          color: Colors.blue,
          colorBrightness: Brightness.dark, 
          disabledColor: Colors.blueGrey,
          highlightColor: Colors.red,
          padding: EdgeInsets.symmetric(
              horizontal: 8.0, vertical: 5.0)
        )

似乎 flutter 的 View 代码需要在每个组件中内置样式?我怎样才能让它完全分开?类似于CSS(几乎)为网络所做的事情?看起来代码会更简洁,我也可以重用我的样式。

我可以为自定义亮度创建类似 Brightness 类的子类,但这似乎有点矫枉过正。

另外,今天发现这个包让我更接近我想要的 - https://pub.dev/packages/division

理想情况下,有哪些好的做法可以将样式与 flutter 中的 View 代码分开?

提前致谢 !

最佳答案

解决方案 1

您可以创建一个可重用的 FlattButon使用默认样式,您可以随时覆盖样式:

例子:

class MyFlatButton extends StatelessWidget {
  const MyFlatButton({
    Key key,
    @required this.text,
    @required this.onPressed,
    this.color = Colors.blue, // set default Values
    this.colorBrightness = Brightness.dark, //
    this.disabledColor = Colors.blueGrey, //
    this.highlightColor = Colors.red, //
    this.padding =
        const EdgeInsets.symmetric(horizontal: 8.0, vertical: 5.0), //
  }) : super(key: key);

  final String text;
  final VoidCallback onPressed;
  final Color color;
  final Brightness colorBrightness;
  final MaterialColor disabledColor;
  final MaterialColor highlightColor;
  final EdgeInsetsGeometry padding;
  @override
  Widget build(BuildContext context) {
    return FlatButton(
      child: Text(
        '$text',
      ),
      onPressed: onPressed,
      color: color,
      colorBrightness: colorBrightness,
      disabledColor: disabledColor,
      highlightColor: highlightColor,
      padding: padding,
    );
  }
}

你可以在你的应用程序中像 MyFlatButton 而不是 FlatButton 一样使用它
  child: MyFlatButton(
    text: 'Hello',
    onPressed: () {},
    color: Colors.cyan,
  ),

解决方案 2

如果您想“完全”分离样式,您可以创建一个包含样式的额外类
class MyFlatButton2 extends StatelessWidget {
  const MyFlatButton2({
    Key key,
    @required this.text,
    @required this.onPressed,
    this.styles = const ButtonStyles(),
  }) : super(key: key);

  final String text;
  final VoidCallback onPressed;
  final ButtonStyles styles;
  @override
  Widget build(BuildContext context) {
    final btnStyles = ButtonStyles();
    return FlatButton(
      child: Text('$text'),
      onPressed: onPressed,
      color: styles.color ?? btnStyles.color,
      colorBrightness: styles.colorBrightness ?? btnStyles.colorBrightness,
      disabledColor: styles.disabledColor ?? btnStyles.disabledColor, 
      highlightColor: styles.highlightColor ?? btnStyles.highlightColor, 
      padding: styles.padding ?? btnStyles.padding,
    ); 
  }
}

// Styles Class
class ButtonStyles {
  final Color color;
  final Brightness colorBrightness;
  final MaterialColor disabledColor;
  final MaterialColor highlightColor;
  final EdgeInsetsGeometry padding;

  const ButtonStyles({
    this.color = Colors.blue, // set default Values
    this.colorBrightness = Brightness.dark, //
    this.disabledColor = Colors.blueGrey, //
    this.highlightColor = Colors.red, //
    this.padding =
        const EdgeInsets.symmetric(horizontal: 8.0, vertical: 5.0), //
  });
}

然后你可以使用它
使用默认样式
  child: MyFlatButton2(
    text: 'Hello',
    onPressed: () {},
  ),

或者你可以覆盖样式
  child: MyFlatButton2(
    text: 'Hello',
    onPressed: () {},
    styles: ButtonStyles(
      colorBrightness: Brightness.dark,
      color: Colors.green,
      padding: EdgeInsets.all(20),
    ),
  ),

额外 , 你也可以有一些带有预定义样式的命名构造函数
  factory ButtonStyles.predefinedStyle1() => ButtonStyles(
        color: Colors.green, // set default Values
        colorBrightness: Brightness.light, //
        disabledColor: Colors.red, //
        highlightColor: Colors.red, //
        padding: const EdgeInsets.symmetric(horizontal: 18.0, vertical: 15.0),
  );

进而
  child: MyFlatButton2(
    text: 'Hello',
    onPressed: () {},
    styles: ButtonStyles.predefinedStyle1(),
  ),

关于flutter - 保持 Flutter 的样式独立于 View 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998968/

相关文章:

flutter - 将项目动态添加到小部件不会更新小部件的高度

android - Flutter中的"Error: Member not found: ' packageRoot ' "

flutter - 每次都需要热加载flutter应用程序以便从Firebase实时数据库中获取数据

flutter - 如何在 flutter 中将音频字节转换为字符串

flutter - Flutter中如何实现自定义对话框?

flutter - _typeerror(类型 'int' 不是类型 'double' 的子类型)

Flutter workmanager 插件在运行任务时无法与任何其他插件一起使用

dart - 如何在 dart flutter 中从 statefulwidget 类获取变量到状态类?

flutter - 我们如何使用Flutter中的底部导航栏触发有状态的小部件来进行导航自身的重建?

flutter - 缺口 BottomAppBar flutter