Flutter, automatically add Semantics value to all Text Widgets

107 views Asked by At

I need to add Semantics value to all Text Widgets, Is there a way to do it automatically, to avoid manual addition?

1

There are 1 answers

0
fvillalba On

There is no automatic way, but you can make your own text input that wraps a flutter text and reuse it. Here is a base example:

import 'package:flutter/material.dart';

class SemanticTextField extends StatelessWidget {
  final String semanticLabel;

  final Function(String)? onChanged;
  const SemanticTextField({
    Key? key,
    required this.semanticLabel,
    this.onChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Semantics(
      container: true,
      textField: true,
      label: semanticLabel,
      child: TextFormField(
        onChanged: onChanged,
      ),
    );
  }
}