How To Disable Text Field Using Flutter Android Dart

admin_img Posted By Bajarangi soft , Posted On 06-11-2020

In this Google flutter code example we are going to learn how to disable text field input in flutter.

How To Disable Text Field Using Flutter Android Dart

Disable Text Field
Complete Code For Disable Text Field In Flutter
main.dart

import 'package:flutter/material.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() {
    return new MyHomePageState();
  }
}

class MyHomePageState extends State<MyHomePage> {
  TextEditingController _textFieldController = TextEditingController();
  bool _isEnabled = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.orange,
        title: Text('Disable TextField'),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Expanded(
              child: Center(
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 25.0),
                  child: TextField(
                    controller: _textFieldController,
                    enabled: _isEnabled,
                    decoration: InputDecoration(hintText: "TextField"),
                  ),
                ),
              ),
            ),
            RaisedButton(
              onPressed: () => setState(() => _isEnabled = !_isEnabled),
              child: Text(_isEnabled ? "Disable" : "Enable"),
              color: Colors.orange,
              textColor: Colors.white,
            ),
            SizedBox(height: 12.0),
          ],
        ),
      ),
    );
  }
}

Related Post