Want to disable a text edit field in flutter? This isn’t a feature that is currently provided by the framework, but you can use a FocusScope to prevent a TextFormField from requesting focus.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import 'package:flutter/material.dart'; void main() { runApp(new MaterialApp( home: new HomePage(), )); } class HomePage extends StatefulWidget { HomePageState createState() => new HomePageState(); } class HomePageState extends State<HomePage> { TextEditingController _controller = new TextEditingController(); bool _enabled = false; @override Widget build(BuildContext context) { ThemeData theme = Theme.of(context); return new Scaffold( appBar: new AppBar( title: new Text('Disabled Text'), ), floatingActionButton: new FloatingActionButton( child: new Icon(Icons.free_breakfast), onPressed: () { setState(() { _enabled = !_enabled; }); } ), body: new Center( child: new Container( margin: const EdgeInsets.all(10.0), child: _enabled ? new TextFormField(controller: _controller) : new FocusScope( node: new FocusScopeNode(), child: new TextFormField( controller: _controller, style: theme.textTheme.subhead.copyWith( color: theme.disabledColor, ), decoration: new InputDecoration( hintText: _enabled ? 'Type something' : 'You cannot focus me', ), ), ), ), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.