在小部件之间共享尺寸 [英] Share dimensions between widgets

查看:69
本文介绍了在小部件之间共享尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter中,我们可以使用 Themes 共享颜色和字体样式。
https://flutter.io/docs/cookbook/design/themes

In Flutter we can use Themes to share colors and font styles. https://flutter.io/docs/cookbook/design/themes

是否存在可以以类似方式使用的最佳实践来共享边距,内边距,宽度或高度等值?

Is there an existing best practice that we can use in a similar manner share values such as margins, paddings and widths or heights?

最好是有助于坚持材料设计准则的东西。

Preferably something that helps stick to the material design guidelines.

推荐答案

定义自定义小部件

最简单也是最优雅的方法是定义自定义小部件,例如内部使用的 MyRaisedButton 具有正确尺寸的 RaisedButton

The easiest and probably most elegant approach is to define custom widgets, like a MyRaisedButton that internally uses the RaisedButton with the right dimensions.

class MyRaisedButton extends StatelessWidget {
  MyRaisedButton({
    this.child,
    this.onPressed,
  });

  final Widget child;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      padding: ...,
      onPressed: onPressed,
      child: child
    );
  }
}

在大多数情况下,效果很好。
但是,如果您仍然希望保持小部件的灵活性(能够将许多自定义选项传递给构造函数),则整个小部件定义很快就会变得很长,因为您需要将所有选项转发给 RaisedButton
在这种情况下,在整个应用程序中实际共享价值才有意义。

This works surprisingly well in most cases. However, if you still want to keep your widgets flexible (being able to pass a lot of customization options to the constructor), your overall widget definition quickly gets very long, because you need to forward all the options to the RaisedButton. In that case, it makes sense to actually share the values throughout the app.

在整个应用程序中实际共享价值

当然,这种方法也是可行的。
由于Flutter的开放性,我们只需要看 主题的实现方式,然后复制该代码以创建功能与 Theme
这是一个精简版本:

Of course, this approach is possible too. Due to Flutter's openness, we can just look at how the Theme is implemented and copy that code to create a custom widget that functions just like a Theme. Here's a boiled-down version:

@immutable
class MyThemeData {
  MyThemeData({
    this.myPadding,
    this.myColor,
    this.myString
  });

  final Padding myPadding;
  final Color myColor;
  final String myString;
}

class MyTheme extends StatelessWidget {
  MyTheme({
    Key key,
    @required this.data,
    @required this.child
  }) : super(key: key);

  final MyThemeData data;
  final Widget child;

  static MyThemeData of(BuildContext context) {
    return (context.ancestorWidgetOfExactType(MyTheme) as MyTheme)?.data;
  }

  @override
  Widget build(BuildContext context) => child;
}

现在,您只需包装 MaterialApp MyTheme 小部件中:

Now, you can just wrap the MaterialApp in a MyTheme widget:

MyTheme(
  data: MyThemeData(
    myPadding: ...,
    myColor: ...,
    ...
  ),
  child: ... (here goes the MaterialApp)
)

然后在应用程序中的任何位置都可以编写 MyTheme.of(context).myPadding

您可以根据需要修改 MyThemeData 类,存储您想要的任何东西。

Then anywhere in your app, you can write MyTheme.of(context).myPadding.
You can adapt the MyThemeData class to your needs, storing anything you want.

这篇关于在小部件之间共享尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆