Skip to main content

type_anything 0.1.2 copy "type_anything: ^0.1.2" to clipboard
type_anything: ^0.1.2 copied to clipboard

Universal Spotlight, Shortcut Tokenizer & Rich Text Input Controls with Natural Syntax Parsing for Flutter.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:type_anything/type_anything.dart';

void main() {
  runApp(const TypeAnythingExampleApp());
}

class TypeAnythingExampleApp extends StatelessWidget {
  const TypeAnythingExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Type Anything Showcase',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFF6366F1),
        ),
      ),
      darkTheme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(
          seedColor: const Color(0xFF6366F1),
          brightness: Brightness.dark,
        ),
      ),
      home: const TypeAnythingHomePage(),
    );
  }
}

class SampleTaskProvider extends TypeAnythingProvider {
  @override
  List<ShortcutRule> get rules => const [
        ShortcutRule(
          ruleType: 'project',
          prefix: '#',
          defaultColor: Color(0xFF2563EB),
        ),
        ShortcutRule(
          ruleType: 'tag',
          prefix: '@',
          isMultiple: true,
          defaultColor: Color(0xFF059669),
        ),
        BuiltinRules.priority,
      ];

  final List<TypeMatchItem> _projects = const [
    TypeMatchItem(
      id: 'p1',
      label: 'Mobile App',
      subtitle: 'Flutter Workspace',
      searchKey: 'mobile app',
      icon: Icons.phone_iphone,
      color: Color(0xFF2563EB),
      value: 'proj_mobile',
    ),
    TypeMatchItem(
      id: 'p2',
      label: 'Website Redesign',
      subtitle: 'Landing & Blog',
      searchKey: 'website redesign',
      icon: Icons.web,
      color: Color(0xFF2563EB),
      value: 'proj_web',
    ),
  ];

  final List<TypeMatchItem> _tags = const [
    TypeMatchItem(
      id: 't1',
      label: 'urgent',
      searchKey: 'urgent',
      icon: Icons.label_important,
      color: Color(0xFFDC2626),
      value: 'tag_urgent',
    ),
    TypeMatchItem(
      id: 't2',
      label: 'feature',
      searchKey: 'feature',
      icon: Icons.star,
      color: Color(0xFF059669),
      value: 'tag_feature',
    ),
  ];

  final List<TypeMatchItem> _priorities = const [
    TypeMatchItem(
      id: 'pr1',
      label: '1 (High Priority)',
      shortLabel: '1',
      searchKey: '1 high',
      icon: Icons.priority_high,
      color: Color(0xFFDC2626),
      value: 'priority_high',
      isIntent: true,
    ),
    TypeMatchItem(
      id: 'pr2',
      label: '2 (Medium Priority)',
      shortLabel: '2',
      searchKey: '2 medium',
      icon: Icons.remove,
      color: Color(0xFFD97706),
      value: 'priority_med',
      isIntent: true,
    ),
  ];

  @override
  Future<List<TypeMatchItem>> search(String ruleType, String query) async {
    final clean = query.trim().toLowerCase();

    if (ruleType == 'project') {
      if (clean.isEmpty) return _projects;
      return _projects
          .where((p) =>
              p.searchKey.contains(clean) ||
              p.label.toLowerCase().contains(clean))
          .toList();
    } else if (ruleType == 'tag') {
      if (clean.isEmpty) return _tags;
      return _tags
          .where((t) =>
              t.searchKey.contains(clean) ||
              t.label.toLowerCase().contains(clean))
          .toList();
    } else if (ruleType == 'priority') {
      if (clean.isEmpty) return _priorities;
      return _priorities.where((p) => p.searchKey.contains(clean)).toList();
    }
    return [];
  }
}

class TypeAnythingHomePage extends StatefulWidget {
  const TypeAnythingHomePage({super.key});

  @override
  State<TypeAnythingHomePage> createState() => _TypeAnythingHomePageState();
}

class _TypeAnythingHomePageState extends State<TypeAnythingHomePage> {
  late final SampleTaskProvider _provider;
  late final TypeEditingController _controller;
  TypeDetectionResult? _lastExtractedResult;

  @override
  void initState() {
    super.initState();
    _provider = SampleTaskProvider();
    _controller = TypeEditingController(
      provider: _provider,
      text: 'Submit App Store update #Mobile App !1 @urgent',
    );
    _provider.recordMatchedCandidate('project', 'mobile app');
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Future<void> _extractStructuredData() async {
    final tokenizer = TypeTokenizer(provider: _provider);
    final result = await tokenizer.extract(_controller.text);
    setState(() {
      _lastExtractedResult = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    final cs = Theme.of(context).colorScheme;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Type Anything Showcase'),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 680),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text(
                  'Quick-Add Input Field',
                  style: Theme.of(context).textTheme.titleLarge?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                ),
                const SizedBox(height: 8),
                Text(
                  'Type "#" for projects (multi-word supported), "@" for tags, or "!" for priority.',
                  style: TextStyle(color: cs.onSurfaceVariant),
                ),
                const SizedBox(height: 20),
                TypeAnythingField(
                  controller: _controller,
                  placeholder:
                      'What needs to be done? e.g. Fix issue #Mobile App @urgent',
                ),
                const SizedBox(height: 16),
                FilledButton.icon(
                  onPressed: _extractStructuredData,
                  icon: const Icon(Icons.bolt),
                  label: const Text('Extract Structured Result'),
                ),
                const SizedBox(height: 24),
                if (_lastExtractedResult != null) ...[
                  Card(
                    elevation: 0,
                    color: cs.surfaceContainerHighest.withValues(alpha: 0.4),
                    child: Padding(
                      padding: const EdgeInsets.all(16),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            'Extraction Result:',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: cs.primary,
                            ),
                          ),
                          const SizedBox(height: 8),
                          Text(
                              'Clean Title: ${_lastExtractedResult!.cleanTitle}'),
                          const Divider(),
                          Text(
                              'Entities (${_lastExtractedResult!.entities.length}):'),
                          const SizedBox(height: 4),
                          for (final entity in _lastExtractedResult!.entities)
                            Padding(
                              padding:
                                  const EdgeInsets.only(left: 8, bottom: 4),
                              child: Text(
                                '• [${entity.ruleType}] "${entity.rawToken}" -> Value: ${entity.value}',
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ],
              ],
            ),
          ),
        ),
      ),
    );
  }
}
1
likes
150
points
165
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Universal Spotlight, Shortcut Tokenizer & Rich Text Input Controls with Natural Syntax Parsing for Flutter.

Homepage
Repository (GitHub)
View/report issues

Topics

#textfield #autocomplete #mentions #spotlight #rich-text

License

MIT (license)

Dependencies

flutter, intl

More

Packages that depend on type_anything