android_system_font_weight 0.4.0 copy "android_system_font_weight: ^0.4.0" to clipboard
android_system_font_weight: ^0.4.0 copied to clipboard

PlatformAndroid

同步采集安卓系统字重原始 Map,提供 OPPO、小米、vivo、荣耀的取值参考函数。 调用方可自定义兼容策略;无法确定字重时返回 null,保留原有字体样式。

example/lib/main.dart

import 'dart:convert';

import 'package:android_system_font_weight/android_system_font_weight.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 启动字重读取和原生文字对比示例。
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const FontWeightExample());
}

/// 展示原始采集数据及字重应用效果。
class FontWeightExample extends StatelessWidget {
  /// 创建字重示例应用。
  const FontWeightExample({super.key});

  /// 构建示例主题与首页。
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xff365cbb)),
        scaffoldBackgroundColor: const Color(0xfff3f5f9),
      ),
      home: const _FontWeightPage(),
    );
  }
}

class _FontWeightPage extends StatefulWidget {
  const _FontWeightPage();

  @override
  State<_FontWeightPage> createState() => _FontWeightPageState();
}

class _FontWeightPageState extends State<_FontWeightPage> {
  static const _sample = '永字八法,系统字重\nAndroid Flutter 0123456789';
  Map<String, String> _raw = const {};
  double? _weight;
  int _elapsedMs = 0;
  int _revision = 0;

  @override
  void initState() {
    super.initState();
    _read();
  }

  void _read() {
    final watch = Stopwatch()..start();
    _raw = AndroidSystemFontWeight.getRawData(refresh: true);
    // getWght 内部复用刚采集的缓存,调用方不需要传入 Map。
    _weight = AndroidSystemFontWeight.getWght();
    _elapsedMs = watch.elapsedMilliseconds;
    _revision++;
    debugPrint(
      'FontWeightSample: ${jsonEncode({'device': '${_raw['manufacturer']} ${_raw['model']}', 'sdk': _raw['sdk'], 'probeStatus': _raw['probeStatus'], 'probeNativeWght': _raw['probeNativeWght'], 'getWght': _weight, 'entryCount': _raw.length, 'elapsedMs': _elapsedMs})}',
    );
    for (final entry in _raw.entries) {
      debugPrint('FontWeightRaw: ${entry.key}=${entry.value}');
    }
  }

  Widget _card(String title, Widget child) => Card(
    color: Colors.white,
    margin: const EdgeInsets.only(bottom: 12),
    child: Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            title,
            style: const TextStyle(fontSize: 14, color: Colors.black54),
          ),
          const SizedBox(height: 12),
          child,
        ],
      ),
    ),
  );

  @override
  Widget build(BuildContext context) {
    final rawJson = const JsonEncoder.withIndent('  ').convert(_raw);
    return Scaffold(
      appBar: AppBar(title: const Text('系统字重测试')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _card(
            '${_raw['manufacturer'] ?? '未读取'} ${_raw['model'] ?? ''}',
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  _weight == null ? '保留默认字重' : 'wght ${_weight!.round()}',
                  style: const TextStyle(
                    fontSize: 30,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 8),
                Text(
                  '探针:${_raw['probeStatus'] ?? '无数据'} · '
                  'Android API ${_raw['sdk'] ?? '?'} · $_elapsedMs ms',
                ),
                const SizedBox(height: 12),
                FilledButton.icon(
                  onPressed: () => setState(_read),
                  icon: const Icon(Icons.refresh),
                  label: const Text('重新读取'),
                ),
                const SizedBox(height: 8),
                const Text('修改系统字重后,返回这里点击重新读取。'),
              ],
            ),
          ),
          _card(
            'Flutter 默认字重',
            const Text(
              _sample,
              style: TextStyle(fontSize: 22, color: Colors.black),
            ),
          ),
          _card(
            'Flutter 应用 getWght()',
            Text(
              _sample,
              style: TextStyle(
                fontSize: 22,
                color: Colors.black,
                fontVariations: _weight == null
                    ? null
                    : [FontVariation('wght', _weight!)],
              ),
            ),
          ),
          _card(
            '原生 Android TextView',
            SizedBox(
              height: 84,
              child: AndroidView(
                key: ValueKey(_revision),
                viewType: 'font-weight/native-sample',
              ),
            ),
          ),
          _card(
            '原始数据 · ${_raw.length} 项',
            Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                OutlinedButton.icon(
                  onPressed: () =>
                      Clipboard.setData(ClipboardData(text: rawJson)),
                  icon: const Icon(Icons.copy),
                  label: const Text('复制原始数据'),
                ),
                const SizedBox(height: 12),
                SelectableText(rawJson, style: const TextStyle(fontSize: 12)),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
0
likes
145
points
141
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

同步采集安卓系统字重原始 Map,提供 OPPO、小米、vivo、荣耀的取值参考函数。 调用方可自定义兼容策略;无法确定字重时返回 null,保留原有字体样式。

License

MIT (license)

Dependencies

flutter, jni

More

Packages that depend on android_system_font_weight

Packages that implement android_system_font_weight