lasso_text_detector 0.0.1
lasso_text_detector: ^0.0.1 copied to clipboard
A Flutter package to detect and extract text by circling it with a lasso gesture.
import 'package:flutter/material.dart';
import 'package:lasso_text_detector/lasso_text_detector.dart';
void main() {
runApp(MaterialApp(home: LassoExample()));
}
class LassoExample extends StatefulWidget {
const LassoExample({super.key});
@override
State<LassoExample> createState() => _LassoExampleState();
}
class _LassoExampleState extends State<LassoExample> {
final LassoTextController _controller = LassoTextController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _handleAction() async {
switch (_controller.value) {
case LassoState.none:
_controller.start();
break;
case LassoState.start:
final result = await _controller.end();
if (mounted && result != null && result.isNotEmpty) {
_showResultSheet(result);
}
break;
case LassoState.end:
_controller.reset();
break;
}
}
void _showResultSheet(List<String> texts) {
showModalBottomSheet(
context: context,
builder: (context) => Container(
padding: const EdgeInsets.all(16),
width: double.infinity,
child: SelectionArea(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Detected Text:',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Text(texts.join('\n')),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
),
],
),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Lasso Text Detector'),
actions: [
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () => _controller.reset(),
),
],
),
body: LassoTextDetector(
controller: _controller,
lassoColor: Colors.blueAccent,
child: ListView(
padding: const EdgeInsets.all(24),
children: [
const Text(
'Try to circle the texts below!',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const Divider(height: 40),
const Text('This is a standard Text widget.'),
const SizedBox(height: 20),
RichText(
text: const TextSpan(
style: TextStyle(color: Colors.black, fontSize: 18),
children: [
TextSpan(text: 'Mixed '),
TextSpan(
text: 'RichText ',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
TextSpan(text: 'styles.'),
],
),
),
const SizedBox(height: 20),
SelectionArea(
child: const Text(
'Text wrapped by SelectionArea can also be detected by lasso.',
style: TextStyle(color: Colors.blueGrey),
),
),
const SizedBox(height: 40),
],
),
),
floatingActionButton: ValueListenableBuilder<LassoState>(
valueListenable: _controller,
builder: (context, state, _) {
final label = switch (state) {
LassoState.none => 'Start Lasso',
LassoState.start => 'Confirm',
LassoState.end => 'Reset',
};
final icon = switch (state) {
LassoState.none => Icons.gesture,
LassoState.start => Icons.check,
LassoState.end => Icons.refresh,
};
final color = switch (state) {
LassoState.start => Colors.green,
_ => null,
};
return FloatingActionButton.extended(
onPressed: _handleAction,
label: Text(label),
icon: Icon(icon),
backgroundColor: color,
);
},
),
);
}
}