force_english_ime 0.0.4
force_english_ime: ^0.0.4 copied to clipboard
A Windows plugin for Flutter that forces English input mode by completely disabling IME. Prevents users from switching input methods via shortcuts like Shift or Ctrl+Space.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:force_english_ime/force_english_ime.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String _platformVersion = 'Unknown';
String _imeStatus = 'Unknown';
final _forceEnglishImePlugin = ForceEnglishIme();
final _emailController = TextEditingController();
final _normalController = TextEditingController();
StreamSubscription<Map<String, dynamic>>? _imeEventSub;
final List<String> _imeEvents = [];
@override
void initState() {
super.initState();
initPlatformState();
_listenImeEvents();
}
@override
void dispose() {
_imeEventSub?.cancel();
_emailController.dispose();
_normalController.dispose();
super.dispose();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await _forceEnglishImePlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
_checkImeStatus();
}
void _listenImeEvents() {
_imeEventSub = _forceEnglishImePlugin.onImeStateChanged.listen((event) {
final type = event['type'] as String;
String message;
switch (type) {
case 'conversionModeChanged':
final isAlpha = event['isAlphanumeric'] as bool;
message = 'Mode: ${isAlpha ? "English" : "Native"}';
break;
case 'openStatusChanged':
final isOpen = event['isOpen'] as bool;
message = 'IME ${isOpen ? "Opened" : "Closed"}';
break;
case 'inputLanguageChanged':
final tag = event['languageTag'] as String;
message = 'Language: $tag';
break;
default:
message = 'Unknown event: $type';
}
setState(() {
_imeEvents.insert(0, message);
if (_imeEvents.length > 20) {
_imeEvents.removeLast();
}
});
});
}
Future<void> _checkImeStatus() async {
try {
final isEnglish = await _forceEnglishImePlugin.isEnglishIme();
setState(() {
_imeStatus = isEnglish ? 'English Mode' : 'Chinese/IME Mode';
});
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('IME Status: $_imeStatus')));
}
} catch (e) {
setState(() {
_imeStatus = 'Error: $e';
});
}
}
Future<void> _forceEnglish() async {
try {
final success = await _forceEnglishImePlugin.forceEnglishInput();
if (success) {
_checkImeStatus();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Forced to English input')),
);
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
}
Future<void> _restore() async {
try {
final success = await _forceEnglishImePlugin.restoreOriginalIme();
if (success) {
_checkImeStatus();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Restored original IME')),
);
}
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
}
Future<void> _cancelForceEnglish() async {
try {
final success = await _forceEnglishImePlugin.cancelForceEnglish();
if (success && mounted) {
_checkImeStatus();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('IME re-enabled (fresh context)')),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error: $e')));
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Force English IME Example')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Running on: $_platformVersion'),
const SizedBox(height: 8),
Text('Current IME Status: $_imeStatus'),
const SizedBox(height: 24),
const Text(
'Email Input (Force English):',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Focus(
onFocusChange: (hasFocus) {
if (hasFocus) {
_forceEnglish();
} else {
_restore();
}
},
child: TextField(
controller: _emailController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter email (English only)',
labelText: 'Email',
),
keyboardType: TextInputType.emailAddress,
),
),
const SizedBox(height: 24),
const Text(
'Normal Input (No restriction):',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
TextField(
controller: _normalController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Enter anything',
labelText: 'Normal Input',
),
),
const SizedBox(height: 24),
Wrap(
spacing: 12,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: _forceEnglish,
child: const Text('Force English'),
),
ElevatedButton(
onPressed: _restore,
child: const Text('Restore IME'),
),
ElevatedButton(
onPressed: _cancelForceEnglish,
child: const Text('Cancel Force'),
),
ElevatedButton(
onPressed: _checkImeStatus,
child: const Text('Check Status'),
),
],
),
const SizedBox(height: 24),
const Text(
'IME State Change Events:',
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Container(
width: double.infinity,
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4),
),
child: _imeEvents.isEmpty
? const Center(
child: Text(
'No events yet.\nToggle IME to see events here.',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.grey),
),
)
: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _imeEvents.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Text(
_imeEvents[index],
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 13,
),
),
);
},
),
),
],
),
),
);
}
}