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"),
),
);