import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: EditorPage(),
);
}
}
class EditorPage extends StatefulWidget {
@override
State<EditorPage> createState() => _EditorPageState();
}
class _EditorPageState extends State<EditorPage> {
late EditorState editorState;
late WordCountService wordCountService;
@override
void initState() {
super.initState();
editorState = EditorState.blank();
wordCountService = WordCountService(
editorState: editorState,
debounceDuration: const Duration(milliseconds: 300),
);
wordCountService.register();
wordCountService.addListener(_updateCounts);
}
@override
void dispose() {
wordCountService.removeListener(_updateCounts);
wordCountService.stop();
wordCountService.dispose();
editorState.dispose();
super.dispose();
}
void _updateCounts() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Word Counter Demo')),
body: Column(
children: [
Expanded(
child: AppFlowyEditor(
editorState: editorState,
),
),
_buildStatusBar(),
],
),
);
}
Widget _buildStatusBar() {
final doc = wordCountService.documentCounters;
final sel = wordCountService.selectionCounters;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
color: Colors.grey[200],
child: Row(
children: [
Text('Words: ${doc.wordCount}'),
const SizedBox(width: 24),
Text('Characters: ${doc.charCount}'),
if (sel.wordCount > 0) ..[
const SizedBox(width: 24),
Text(
'Selected: ${sel.wordCount} words, ${sel.charCount} chars',
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
],
),
);
}
}