import 'package:flutter/material.dart'; import '../api/oa_client.dart'; import '../theme.dart'; import '../widgets/beian_footer.dart'; class LegalPage extends StatefulWidget { const LegalPage({super.key, required this.api, required this.kind}); final OaClient api; final String kind; @override State createState() => _LegalPageState(); } class _LegalPageState extends State { String _title = ''; String _meta = ''; List> _sections = []; String _error = ''; bool _loading = true; @override void initState() { super.initState(); _title = widget.kind == 'privacy' ? '隐私政策' : '用户协议'; _load(); } Future _load() async { try { final data = await widget.api.get('/legal/${widget.kind}'); if (data is! Map) throw ApiException('文档格式错误'); final paras = data['paragraphs']; final sections = >[]; if (paras is List) { for (final row in paras) { if (row is Map) { sections.add({ 'heading': '${row['heading'] ?? ''}', 'body': '${row['body'] ?? ''}', }); } } } if (!mounted) return; setState(() { _title = '${data['title'] ?? _title}'; _meta = '${data['operator'] ?? ''} · 更新日期 ${data['updatedAt'] ?? ''}'; _sections = sections; _loading = false; }); } catch (e) { if (!mounted) return; setState(() { _error = '$e'; _loading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar(title: Text(_title)), body: _loading ? const Center(child: CircularProgressIndicator(color: kBind)) : _error.isNotEmpty ? Center(child: Padding(padding: const EdgeInsets.all(24), child: Text(_error, style: const TextStyle(color: kDanger)))) : ListView( padding: const EdgeInsets.fromLTRB(20, 12, 20, 40), children: [ if (_meta.trim().isNotEmpty) Padding( padding: const EdgeInsets.only(bottom: 16), child: Text(_meta, style: const TextStyle(color: kMute, fontSize: 12, height: 1.5)), ), for (final s in _sections) ...[ if ((s['heading'] ?? '').isNotEmpty) Padding( padding: const EdgeInsets.only(top: 8, bottom: 8), child: Text(s['heading']!, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600)), ), Text(s['body'] ?? '', style: const TextStyle(fontSize: 14, height: 1.7, color: Color(0xFF334155))), const SizedBox(height: 12), ], const BeianFooter(), ], ), ); } }