Files
openchamber-android-native/lib/widgets/model_picker.dart
T
blumia 7b5bccf28f feat: English and Chinese, and selectable chat text
- settings → general → language picks system, Chinese or English; the
  choice is stored on the device and read before the first frame
- our Chinese strings are translated through tr()/trArgs() and AppText,
  with the English in lib/l10n/en_*.dart; a missing entry falls back to
  Chinese and a test checks that every literal in lib has one
- AppText rebuilds on a language change (it depends on the resolved
  locale), so const text and whole pages switch at once; the built-in
  date/time pickers and selection menu follow the same language
- server and user data (titles, messages, prompts, paths) is never
  translated
- the chat list sits in a SelectionArea: long-press selects message text
  and shows the copy/select-all menu
- bump to 0.5.2
2026-11-26 09:04:25 +08:00

327 lines
10 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The model picker: favourites, recents, models grouped by provider with their
// context size, and — when a model has them — its thinking levels, chosen in
// the same sheet because model and effort are one selection.
import 'package:flutter/material.dart';
import '../api/models.dart';
import '../model_prefs.dart';
import '../state.dart';
import '../l10n.dart';
Future<void> showModelPickerSheet(
BuildContext context, {
required AppState app,
required ModelRef? model,
required ValueChanged<ModelRef> onModel,
}) async {
app.refreshModels();
await ModelPrefs.instance.ensureLoaded();
if (!context.mounted) return;
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
builder: (context) => DraggableScrollableSheet(
expand: false,
initialChildSize: 0.85,
maxChildSize: 0.95,
builder: (context, controller) => _PickerSheet(
app: app,
model: model,
scrollController: controller,
onModel: onModel,
),
),
);
}
class _PickerSheet extends StatefulWidget {
final AppState app;
final ModelRef? model;
final ScrollController scrollController;
final ValueChanged<ModelRef> onModel;
const _PickerSheet({
required this.app,
required this.model,
required this.scrollController,
required this.onModel,
});
@override
State<_PickerSheet> createState() => _PickerSheetState();
}
class _PickerSheetState extends State<_PickerSheet> {
String _filter = '';
final Set<String> _collapsed = {};
/// The model whose thinking level the bottom panel is offering; null hides
/// the panel.
ModelInfo? _effortModel;
String? _effortVariant;
ModelPrefs get _prefs => ModelPrefs.instance;
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: widget.app,
builder: (context, _) => Column(
children: [
const Padding(
padding: EdgeInsets.fromLTRB(12, 12, 12, 4),
child: AppText('选择模型',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
),
Padding(
padding: const EdgeInsets.all(12),
child: TextField(
onChanged: (v) => setState(() => _filter = v),
decoration: InputDecoration(
hintText: tr('搜索'),
border: const OutlineInputBorder(),
isDense: true),
),
),
Expanded(child: _models()),
if (_effortModel != null) _effortPanel(_effortModel!),
],
),
);
}
Widget _models() {
final all = widget.app.modelList;
if (all.isEmpty) {
return const Center(child: AppText('正在载入模型…'));
}
final filter = _filter.trim().toLowerCase();
final matching = filter.isEmpty
? all
: [
for (final m in all)
if (m.name.toLowerCase().contains(filter) ||
m.id.toLowerCase().contains(filter))
m,
];
if (matching.isEmpty) return const Center(child: AppText('没有结果'));
final byKey = {
for (final m in all) ModelPrefs.keyFor(m.providerId, m.id): m,
};
final favorites = [
for (final key in _prefs.favorites)
if (byKey[key] != null) byKey[key]!,
]..sort((a, b) => a.name.compareTo(b.name));
final recents = [
for (final key in _prefs.recents)
if (byKey[key] != null && !_prefs.favorites.contains(key)) byKey[key]!,
];
final providers = <String, List<ModelInfo>>{};
for (final model in matching) {
providers.putIfAbsent(model.providerId, () => []).add(model);
}
final children = <Widget>[];
if (filter.isEmpty) {
if (favorites.isNotEmpty) {
children.add(_sectionHeader(tr('收藏'), 'favorites', count: favorites.length));
if (!_collapsed.contains('favorites')) {
for (final model in favorites) {
children.add(_modelRow(model));
}
}
}
if (recents.isNotEmpty) {
children.add(_sectionHeader(tr('最近'), 'recents', count: recents.length));
if (!_collapsed.contains('recents')) {
for (final model in recents) {
children.add(_modelRow(model));
}
}
}
}
for (final entry in providers.entries) {
final label = entry.key.isEmpty ? tr('其它') : entry.key;
children.add(_sectionHeader(label, 'provider:${entry.key}',
count: entry.value.length));
if (!_collapsed.contains('provider:${entry.key}')) {
for (final model in entry.value) {
children.add(_modelRow(model));
}
}
}
return ListView(controller: widget.scrollController, children: children);
}
Widget _sectionHeader(String label, String key, {int? count}) {
final collapsed = _collapsed.contains(key);
return InkWell(
onTap: () => setState(() {
if (collapsed) {
_collapsed.remove(key);
} else {
_collapsed.add(key);
}
}),
child: Padding(
padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
child: Row(
children: [
Icon(collapsed ? Icons.chevron_right : Icons.expand_more,
size: 16, color: Theme.of(context).hintColor),
const SizedBox(width: 4),
Expanded(
child: AppText(
count == null ? label : trArgs('{0}({1})', [label, count]),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
color: Theme.of(context).hintColor),
),
),
],
),
),
);
}
Widget _modelRow(ModelInfo model) {
final theme = Theme.of(context);
final key = ModelPrefs.keyFor(model.providerId, model.id);
final selected = widget.model != null &&
widget.model!.id == model.id &&
widget.model!.providerId == model.providerId;
final favorite = _prefs.isFavorite(key);
return ListTile(
dense: true,
contentPadding: const EdgeInsets.only(left: 20, right: 4),
title: Row(
children: [
Flexible(
child: AppText(model.name,
maxLines: 1, overflow: TextOverflow.ellipsis),
),
if (model.reasoning)
Padding(
padding: const EdgeInsets.only(left: 4),
child: Icon(Icons.psychology_outlined,
size: 14, color: theme.hintColor),
),
],
),
subtitle: model.context > 0
? AppText(
trArgs('{0} 上下文', [formatTokenCount(model.context)]),
style: TextStyle(fontSize: 11, color: theme.hintColor))
: null,
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (selected)
Padding(
padding: const EdgeInsets.only(right: 4),
child: Icon(Icons.check,
size: 18, color: theme.colorScheme.primary),
),
IconButton(
visualDensity: VisualDensity.compact,
icon: Icon(
favorite ? Icons.star : Icons.star_border,
size: 18,
color: favorite ? theme.colorScheme.primary : theme.hintColor,
),
tooltip: favorite ? tr('取消收藏') : tr('收藏'),
onPressed: () async {
await _prefs.toggleFavorite(key);
if (mounted) setState(() {});
},
),
],
),
onTap: () => _select(model),
);
}
Future<void> _select(ModelInfo model) async {
final key = ModelPrefs.keyFor(model.providerId, model.id);
final remembered = _prefs.effortFor(key);
final variant = remembered != null && model.variants.contains(remembered)
? remembered
: null;
await _prefs.rememberModel(key);
if (!mounted) return;
// Model and effort are one choice: apply the model with its remembered
// level, then let the level be changed without leaving the sheet.
widget.onModel(ModelRef(model.id, model.providerId, variant));
if (model.hasEffort) {
setState(() {
_effortModel = model;
_effortVariant = variant;
});
} else {
Navigator.pop(context);
}
}
Future<void> _pickEffort(ModelInfo model, String? variant) async {
final key = ModelPrefs.keyFor(model.providerId, model.id);
await _prefs.rememberEffort(key, variant);
widget.onModel(ModelRef(model.id, model.providerId, variant));
if (!mounted) return;
Navigator.pop(context);
}
Widget _effortPanel(ModelInfo model) {
final theme = Theme.of(context);
final options = <String?>[null, ...model.variants];
return Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: theme.dividerColor.withValues(alpha: 0.6))),
),
padding: const EdgeInsets.fromLTRB(16, 10, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
const AppText('思考强度',
style: TextStyle(
fontSize: 13, fontWeight: FontWeight.w700)),
const SizedBox(width: 6),
Expanded(
child: AppText(model.name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style:
TextStyle(fontSize: 12, color: theme.hintColor)),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const AppText('完成'),
),
],
),
Wrap(
spacing: 6,
runSpacing: 6,
children: [
for (final option in options)
ChoiceChip(
label: AppText(effortLabel(option),
style: const TextStyle(fontSize: 12.5)),
selected: (_effortVariant ?? '') == (option ?? ''),
onSelected: (_) => _pickEffort(model, option),
),
],
),
],
),
);
}
}