可通过CupertinoTheme.of获取CupertinoThemeData对象。也可以指定主题应用于CupertinoTheme的后代组件。
相关组件
文字样式-TextTheme
后代组件可以通过CupertinoTheme.of获取主题的数据进行使用。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class TextCupertinoTheme extends StatelessWidget {
@override
Widget build(BuildContext context) {
var queryData = CupertinoTheme.of(context).textTheme;
var styles = {
"tabLabelTextStyle: ": queryData.tabLabelTextStyle,
"actionTextStyle: ": queryData.actionTextStyle,
"navActionTextStyle: ": queryData.navActionTextStyle,
"textStyle: ": queryData.textStyle,
"navTitleTextStyle: ": queryData.navTitleTextStyle,
"pickerTextStyle: ": queryData.pickerTextStyle,
"dateTimePickerTextStyle: ": queryData.dateTimePickerTextStyle,
"navLargeTitleTextStyle: ": queryData.navLargeTitleTextStyle,
};
return Container(
child: Column(
children: styles.keys.map((e) => buildItem(e, styles[e])).toList(),
),
);
}
Widget buildItem(String e, TextStyle style) => Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
e,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
Text(
"@toly",
style: style,
)
],
),
),
Divider(
height: 1,
)
],
);
}
CupertinoThemeData的使用
和Theme一样可以通过指定的属性,让它们在后代中共享,不过属性较少。注意如果需要使用主题,不能在当前的context中获取。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class CustomCupertinoTheme extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoTheme(
data: CupertinoThemeData(
primaryColor: Colors.blue,
primaryContrastingColor: Colors.green
),
child: _ChildUseTheme());
}
}
class _ChildUseTheme extends StatelessWidget {
const _ChildUseTheme({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
Container(
width: 50,
height: 50,
color: CupertinoTheme.of(context).primaryContrastingColor,
),
Container(
width: 150,
child: Slider(value: 0.8, onChanged: (v) => {})),
Container( width: 150,child: Divider(color:CupertinoTheme.of(context).primaryContrastingColor,thickness: 1,))
]);
}
}