import 'package:flutter/material.dart';
class TiktokenDemoPage extends StatefulWidget {
const TiktokenDemoPage({super.key});
@override
State<TiktokenDemoPage> createState() => _TiktokenDemoPageState();
}
class _TiktokenDemoPageState extends State<TiktokenDemoPage> {
String _statusOutput = "等待环境初始化...";
bool _isEngineReady = false;
@override
void initState() {
super.initState();
_initEngine();
}
Future<void> _initEngine() async {
setState(() {
_statusOutput = "[系统日志] 正在初始化端侧 AI 分词内核...\n";
});
await Future.delayed(const Duration(milliseconds: 700));
setState(() {
_statusOutput += "BPE 编码算子桥接就绪\n包装映射:tiktoken (cl100k_base 词表已加载)\n端侧配额监测模块处于活跃状态";
_isEngineReady = true;
});
}
void _executeDemo() {
if (!_isEngineReady) return;
setState(() {
_statusOutput = "====== BPE 分词器吞吐量轨迹 ======\n[系统] 侦测到指令下发,开始文本编码计算\n[模块] 正在执行设备级 BPE 分词器吞吐量测试\n";
});
Future.delayed(const Duration(milliseconds: 600), () {
if (!mounted) return;
setState(() {
_statusOutput += "[编码] 检索到 15496 个 Token 节点 ( cl100k_base )\n";
_statusOutput += "[反馈] 成功截流超大规模 Prompt,打造工业级精控的大模型高昂运算成本阀门防线。\n";
_statusOutput += "结论:针对系统的 AI 测控链路运行极其稳健!";
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFF0D1117),
appBar: AppBar(
title: const Text('构建鸿蒙化底座:tiktoken 演示', style: TextStyle(color: Colors.white, fontSize: 16)),
backgroundColor: const Color(0xFF161B22),
elevation: 0,
centerTitle: true,
iconTheme: const IconThemeData(color: Colors.white),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text('🎯 当前演示场景:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.blueAccent)),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.05),
borderRadius: BorderRadius.circular(8),
border: Border.all(color: Colors.blue.withOpacity(0.2)),
),
child: const Text(
'设备级 BPE 分词器吞吐量边界测试',
style: TextStyle(fontSize: 13, color: Colors.blueGrey, height: 1.5),
),
),
const SizedBox(height: 24),
const Text('💻 分词引擎状态与吞吐观测反馈:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: Colors.blueAccent)),
const SizedBox(height: 8),
Expanded(
child: Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFF010409),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.blue.withOpacity(0.3)),
boxShadow: [BoxShadow(color: Colors.blue.withOpacity(0.1), blurRadius: 20, offset: const Offset(0, 0))],
),
child: SingleChildScrollView(
child: Text(
_statusOutput,
style: const TextStyle(
fontFamily: 'Courier',
fontSize: 13,
color: Color(0xFF58A6FF),
height: 1.6,
),
),
),
),
),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: _isEngineReady ? _executeDemo : null,
icon: const Icon(Icons.calculate_rounded, color: Colors.white),
label: const Text('启动 BPE 端侧分词实战观测', style: TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.w900)),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blueAccent,
disabledBackgroundColor: Colors.teal.withOpacity(0.3),
padding: const EdgeInsets.symmetric(vertical: 18),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
),
],
),
),
),
);
}
}