Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created June 9, 2024 22:44
Show Gist options
  • Save ashraf267/8afd3a562ccb8650de7b16b7cba49026 to your computer and use it in GitHub Desktop.
Save ashraf267/8afd3a562ccb8650de7b16b7cba49026 to your computer and use it in GitHub Desktop.
enable or disable btn flutter snippet code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final myController = TextEditingController();
bool enable = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Column(
children: [
SizedBox(
width: 200,
child: TextField(
cursorColor: Colors.red,
controller: myController,
onChanged: (val) {
if (myController.text.isNotEmpty) {
enable = true;
} else {
enable = false;
}
setState(() {
//
});
}),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: (enable)
? () {
print(myController.text);
}
: null,
child: Text('login'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: null,
child: Text('sign-in'),
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment