所以这就是我今天设计简单的颤动按钮的方式-
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)
)
似乎颤振的视图代码要求每个组件都内置样式?我怎样才能完全分开?像什么CSS(几乎)用于网络?似乎代码会更加简洁,我也可以重用我的样式。
我可以将诸如Brightness类之类的子类派生为自定义亮度,但是这似乎有点过头了。
此外,今天还找到了这个软件包,它使我更接近想要的东西-https://pub.dev/packages/division
理想情况下,有什么好的做法可以使样式与视图代码分离而扑朔迷离?
谢谢!
答案 0 :(得分:1)
也许您可能知道,但是您可以在main.dart中声明通用样式,
对于Button主题,
ThemeData(
...
buttonTheme: ButtonThemeData(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0)),
colorScheme: colorSchemeDark,
splashColor: AppColors.themeColor.shade900,
height: 50,
highlightColor: AppColors.themeColor.shade800,
textTheme: ButtonTextTheme.primary,
),
)
对于卡片主题,
ThemeData(
...
cardTheme: CardTheme(
margin: EdgeInsets.only(top: 8, left: 8, right: 8, bottom: 8),
elevation: 6.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
我创建了一个gist here
答案 1 :(得分:1)
解决方案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(),
),
答案 2 :(得分:0)
使用此软件包https://pub.dev/packages/division
它允许您为小部件创建样式,将其存储以供重复使用等等。
final textStyle = TxtStyle()
..textColor(Colors.white)
..fontSize(30)
..bold()
..italic()
..textAlign.start();
Text(
'hi',
style:textStyle
)