Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 我可以在主题数据中设置容器的样式吗?_Flutter_Dart_Containers_Flutter Widget - Fatal编程技术网

Flutter 我可以在主题数据中设置容器的样式吗?

Flutter 我可以在主题数据中设置容器的样式吗?,flutter,dart,containers,flutter-widget,Flutter,Dart,Containers,Flutter Widget,为了在我的容器中形成一个圆角,我可以这样做 Container( decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(20)) ), child: ... ) 但是,每当我创建一个圆角容器时,我都需要一遍又一遍地这样做。我可以在我的主题数据中设置它吗?我可以像下面的代码一样在主题数据中设置appBar主题,但是我需要一些类似的东西,只是用于容器。怎么做 final themeDa

为了在我的容器中形成一个圆角,我可以这样做

Container(
  decoration: BoxDecoration(
    borderRadius: BorderRadius.all(Radius.circular(20))
  ),
  child: ...
)
但是,每当我创建一个圆角容器时,我都需要一遍又一遍地这样做。我可以在我的主题数据中设置它吗?我可以像下面的代码一样在主题数据中设置appBar主题,但是我需要一些类似的东西,只是用于容器。怎么做

final themeData = ThemeData(
  appBarTheme: _appBarTheme,
);

const AppBarTheme _appBarTheme = const AppBarTheme(
  elevation: 2,
  centerTitle: true,
  color: const Color.fromRGBO(245, 245, 245, 1),
  textTheme: TextTheme(
    // center text style
    headline6: TextStyle(color: Colors.black, fontSize: 16),
    // Side text style
    bodyText2: TextStyle(color: Colors.black),
  ),
);

我认为您最好的选择是隔离一个容器,并使用如下示例所示的参数定制这个新的小部件

class CustomContainerWdt extends StatelessWidget {
  final Color? color;
  final Widget child;
  final String text;

  const CustomContainerWdt({Key key, this.color, this.child, this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: color ?? Theme.of(context).primaryColor,
        borderRadius: BorderRadius.all(Radius.circular(20))
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text('$text'),
          child,
        ],
      ),
    );
  }
}

您可以提供一个主题来包装容器以提供主题

   Theme(
      // Create a unique theme with "ThemeData"
      data: ThemeData(
        textTheme: /* Your Text Theme*/,
      ),
      child: Container(
        onPressed: () {},
        child: Text("Your Text Here"),
      ),
    );