|
| 1 | +import 'package:bb_mobile/core/themes/app_theme.dart'; |
| 2 | +import 'package:bb_mobile/core/utils/build_context_x.dart'; |
| 3 | +import 'package:bb_mobile/core/widgets/bottom_sheet/x.dart'; |
| 4 | +import 'package:bb_mobile/core/widgets/text/text.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:gap/gap.dart'; |
| 7 | + |
| 8 | +class DisclosureLink extends StatelessWidget { |
| 9 | + const DisclosureLink({ |
| 10 | + super.key, |
| 11 | + required this.label, |
| 12 | + required this.semanticLabel, |
| 13 | + required this.title, |
| 14 | + required this.body, |
| 15 | + }); |
| 16 | + |
| 17 | + final String label; |
| 18 | + final String semanticLabel; |
| 19 | + final String title; |
| 20 | + final String body; |
| 21 | + |
| 22 | + @override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + final color = context.appColors.onSurfaceVariant; |
| 25 | + |
| 26 | + return Semantics( |
| 27 | + button: true, |
| 28 | + label: semanticLabel, |
| 29 | + child: Material( |
| 30 | + type: MaterialType.transparency, |
| 31 | + child: InkWell( |
| 32 | + onTap: () => |
| 33 | + DisclosureBottomSheet.show(context, title: title, body: body), |
| 34 | + borderRadius: BorderRadius.circular(2), |
| 35 | + child: ConstrainedBox( |
| 36 | + constraints: const BoxConstraints(minHeight: 44), |
| 37 | + child: Row( |
| 38 | + mainAxisSize: MainAxisSize.min, |
| 39 | + children: [ |
| 40 | + Icon(Icons.info_outline, size: 16, color: color), |
| 41 | + const Gap(6), |
| 42 | + Flexible( |
| 43 | + child: BBText( |
| 44 | + label, |
| 45 | + style: context.font.labelSmall, |
| 46 | + color: color, |
| 47 | + ), |
| 48 | + ), |
| 49 | + ], |
| 50 | + ), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ), |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class DisclosureBottomSheet extends StatelessWidget { |
| 59 | + const DisclosureBottomSheet({ |
| 60 | + super.key, |
| 61 | + required this.title, |
| 62 | + required this.body, |
| 63 | + }); |
| 64 | + |
| 65 | + final String title; |
| 66 | + final String body; |
| 67 | + |
| 68 | + static Future<void> show( |
| 69 | + BuildContext context, { |
| 70 | + required String title, |
| 71 | + required String body, |
| 72 | + }) { |
| 73 | + return BlurredBottomSheet.show( |
| 74 | + context: context, |
| 75 | + child: DisclosureBottomSheet(title: title, body: body), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + Widget build(BuildContext context) { |
| 81 | + return Container( |
| 82 | + constraints: BoxConstraints( |
| 83 | + maxHeight: MediaQuery.of(context).size.height * 0.82, |
| 84 | + ), |
| 85 | + decoration: BoxDecoration( |
| 86 | + color: context.appColors.surface, |
| 87 | + borderRadius: const BorderRadius.vertical(top: Radius.circular(8)), |
| 88 | + ), |
| 89 | + child: Column( |
| 90 | + mainAxisSize: MainAxisSize.min, |
| 91 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 92 | + children: [ |
| 93 | + Padding( |
| 94 | + padding: const EdgeInsets.fromLTRB(24, 24, 16, 12), |
| 95 | + child: Row( |
| 96 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 97 | + children: [ |
| 98 | + Expanded( |
| 99 | + child: BBText(title, style: context.font.headlineMedium), |
| 100 | + ), |
| 101 | + IconButton( |
| 102 | + tooltip: context.loc.closeDialogButton, |
| 103 | + onPressed: () => Navigator.of(context).pop(), |
| 104 | + icon: const Icon(Icons.close), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ), |
| 108 | + ), |
| 109 | + Flexible( |
| 110 | + child: SingleChildScrollView( |
| 111 | + padding: const EdgeInsets.fromLTRB(24, 0, 24, 24), |
| 112 | + child: _DisclosureBody(body), |
| 113 | + ), |
| 114 | + ), |
| 115 | + ], |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +class _DisclosureBody extends StatelessWidget { |
| 122 | + const _DisclosureBody(this.body); |
| 123 | + |
| 124 | + final String body; |
| 125 | + |
| 126 | + @override |
| 127 | + Widget build(BuildContext context) { |
| 128 | + final blocks = body.trim().split(RegExp(r'\n\s*\n')); |
| 129 | + final children = <Widget>[]; |
| 130 | + |
| 131 | + for (final block in blocks) { |
| 132 | + if (block.trim().isEmpty) continue; |
| 133 | + if (children.isNotEmpty) { |
| 134 | + children.add(Gap(_spacingBefore(block))); |
| 135 | + } |
| 136 | + children.add(_buildBlock(context, block.trim())); |
| 137 | + } |
| 138 | + |
| 139 | + return Column( |
| 140 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 141 | + children: children, |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + double _spacingBefore(String block) { |
| 146 | + if (block.startsWith('## ')) return 24; |
| 147 | + if (block.startsWith('### ') || block.startsWith('> ')) return 16; |
| 148 | + return 12; |
| 149 | + } |
| 150 | + |
| 151 | + Widget _buildBlock(BuildContext context, String block) { |
| 152 | + if (block.startsWith('## ')) { |
| 153 | + return Semantics( |
| 154 | + header: true, |
| 155 | + child: BBText( |
| 156 | + block.substring(3), |
| 157 | + style: context.font.titleMedium?.copyWith(fontWeight: .w600), |
| 158 | + color: context.appColors.text, |
| 159 | + ), |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + if (block.startsWith('### ')) { |
| 164 | + return Semantics( |
| 165 | + header: true, |
| 166 | + child: BBText( |
| 167 | + block.substring(4), |
| 168 | + style: context.font.bodyLarge?.copyWith(fontWeight: .w600), |
| 169 | + color: context.appColors.text, |
| 170 | + ), |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + final lines = block.split('\n'); |
| 175 | + if (lines.every((line) => line.startsWith('> '))) { |
| 176 | + return _buildCallout( |
| 177 | + context, |
| 178 | + title: lines.first.substring(2), |
| 179 | + body: lines.skip(1).map((line) => line.substring(2)).join('\n'), |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + if (lines.every((line) => line.startsWith('- '))) { |
| 184 | + return _buildBulletList( |
| 185 | + context, |
| 186 | + lines.map((line) => line.substring(2)).toList(), |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + return BBText( |
| 191 | + block, |
| 192 | + style: context.font.bodyMedium, |
| 193 | + color: context.appColors.text, |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + Widget _buildCallout( |
| 198 | + BuildContext context, { |
| 199 | + required String title, |
| 200 | + required String body, |
| 201 | + }) { |
| 202 | + return Container( |
| 203 | + padding: const EdgeInsets.all(16), |
| 204 | + decoration: BoxDecoration( |
| 205 | + color: context.appColors.warningContainer, |
| 206 | + border: Border.all(color: context.appColors.warning), |
| 207 | + borderRadius: BorderRadius.circular(2), |
| 208 | + ), |
| 209 | + child: Row( |
| 210 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 211 | + children: [ |
| 212 | + Icon( |
| 213 | + Icons.warning_amber_rounded, |
| 214 | + size: 22, |
| 215 | + color: context.appColors.warning, |
| 216 | + ), |
| 217 | + const Gap(12), |
| 218 | + Expanded( |
| 219 | + child: Column( |
| 220 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 221 | + children: [ |
| 222 | + BBText( |
| 223 | + title, |
| 224 | + style: context.font.bodyLarge?.copyWith(fontWeight: .w600), |
| 225 | + color: context.appColors.text, |
| 226 | + ), |
| 227 | + const Gap(6), |
| 228 | + BBText( |
| 229 | + body, |
| 230 | + style: context.font.bodyMedium, |
| 231 | + color: context.appColors.text, |
| 232 | + ), |
| 233 | + ], |
| 234 | + ), |
| 235 | + ), |
| 236 | + ], |
| 237 | + ), |
| 238 | + ); |
| 239 | + } |
| 240 | + |
| 241 | + Widget _buildBulletList(BuildContext context, List<String> items) { |
| 242 | + return Column( |
| 243 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 244 | + children: [ |
| 245 | + for (var index = 0; index < items.length; index++) ...[ |
| 246 | + if (index > 0) const Gap(8), |
| 247 | + Row( |
| 248 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 249 | + children: [ |
| 250 | + SizedBox( |
| 251 | + width: 18, |
| 252 | + child: BBText( |
| 253 | + '•', |
| 254 | + style: context.font.bodyMedium, |
| 255 | + color: context.appColors.text, |
| 256 | + ), |
| 257 | + ), |
| 258 | + Expanded(child: _buildBulletText(context, items[index])), |
| 259 | + ], |
| 260 | + ), |
| 261 | + ], |
| 262 | + ], |
| 263 | + ); |
| 264 | + } |
| 265 | + |
| 266 | + Widget _buildBulletText(BuildContext context, String item) { |
| 267 | + final style = context.font.bodyMedium?.copyWith( |
| 268 | + color: context.appColors.text, |
| 269 | + ); |
| 270 | + final match = RegExp(r'^\*\*(.+?)\*\*(.*)$').firstMatch(item); |
| 271 | + |
| 272 | + if (match == null) return Text(item, style: style); |
| 273 | + |
| 274 | + return Text.rich( |
| 275 | + TextSpan( |
| 276 | + style: style, |
| 277 | + children: [ |
| 278 | + TextSpan( |
| 279 | + text: match.group(1), |
| 280 | + style: style?.copyWith(fontWeight: .w600), |
| 281 | + ), |
| 282 | + TextSpan(text: match.group(2)), |
| 283 | + ], |
| 284 | + ), |
| 285 | + ); |
| 286 | + } |
| 287 | +} |
0 commit comments