Home > front end >  Flutter - TextField: No MaterialLocalizations found
Flutter - TextField: No MaterialLocalizations found

Time:06-08

I'm trying to make a screenshot of a TextField including its content. I created a function that returns the TextField wrapped in Material. This function is called in the class to view the TextField and allow the user to enter its content. Then in the `onPressed() of a floating button in the same class I tried making the screenshot.

The textField is viewed in the application but when I click the button, I get the No MaterialLocalizations found error.

If I replaced TextField with Text it works without problems. I don't know how to make it work with TextField??

Here is the code:

class TextFromUserState extends State<TextFromUser> {
  Uint8List? bytes;

  Widget _buildTextField() {
    return Material(
      child: Container(
        child: TextField(
          decoration: InputDecoration(
            fillColor: Colors.grey[300],
            filled: true,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Read User input")),
      body: Column(
        children: <Widget>[
          _buildTextField(),
          if (this.bytes != null) Image.memory(this.bytes!),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.camera_alt),
        label: Text('Capture'),
        onPressed: () async {
          final controller = ScreenshotController();
          final bytes =
              await controller.captureFromWidget(_buildTextField());
          setState(() => this.bytes = bytes);
        },
      ),
    );
  }
}

CodePudding user response:

 Widget _buildTextField() {
    return 
       Container(
        child: Localizations(
      locale: const Locale('en', 'US'),
      delegates: const <LocalizationsDelegate<dynamic>>[
        DefaultWidgetsLocalizations.delegate,
        DefaultMaterialLocalizations.delegate,
      ],
      child: MediaQuery(
        data: const MediaQueryData(),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: Material(
            child: TextField(
          decoration: InputDecoration(
            fillColor: Colors.grey[300],
            filled: true,
          ),
          ),
        ),
      ),
));

i guess there is no MaterialLocalizations widget in the widget tree, which textfeild required must you can give MaterialLocalization of your own like i added above, for detail you can visit here

  • Related