可通过CupertinoTheme.of获取CupertinoThemeData对象。也可以指定主题应用于CupertinoTheme的后代组件。

相关组件

CupertinoApp Theme

文字样式-TextTheme

后代组件可以通过CupertinoTheme.of获取主题的数据进行使用。
image.png

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class TextCupertinoTheme extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. var queryData = CupertinoTheme.of(context).textTheme;
  7. var styles = {
  8. "tabLabelTextStyle: ": queryData.tabLabelTextStyle,
  9. "actionTextStyle: ": queryData.actionTextStyle,
  10. "navActionTextStyle: ": queryData.navActionTextStyle,
  11. "textStyle: ": queryData.textStyle,
  12. "navTitleTextStyle: ": queryData.navTitleTextStyle,
  13. "pickerTextStyle: ": queryData.pickerTextStyle,
  14. "dateTimePickerTextStyle: ": queryData.dateTimePickerTextStyle,
  15. "navLargeTitleTextStyle: ": queryData.navLargeTitleTextStyle,
  16. };
  17. return Container(
  18. child: Column(
  19. children: styles.keys.map((e) => buildItem(e, styles[e])).toList(),
  20. ),
  21. );
  22. }
  23. Widget buildItem(String e, TextStyle style) => Column(
  24. children: <Widget>[
  25. Padding(
  26. padding: const EdgeInsets.all(8.0),
  27. child: Row(
  28. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  29. children: <Widget>[
  30. Text(
  31. e,
  32. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  33. ),
  34. Text(
  35. "@toly",
  36. style: style,
  37. )
  38. ],
  39. ),
  40. ),
  41. Divider(
  42. height: 1,
  43. )
  44. ],
  45. );
  46. }

CupertinoThemeData的使用

和Theme一样可以通过指定的属性,让它们在后代中共享,不过属性较少。注意如果需要使用主题,不能在当前的context中获取。
image.png

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,))
        ]);
  }
}