Last active
June 9, 2021 16:52
-
-
Save sabetAI/db1f911437df968ba8023cae99d65d0d to your computer and use it in GitHub Desktop.
Refactoring InputDecoration does not reproduce behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Column( | |
children: [PasswordField1(), PasswordField2()], | |
), | |
), | |
); | |
} | |
} | |
class SignInDecoration extends InputDecoration { | |
SignInDecoration(BuildContext context, {Widget suffixIcon, String hintText}) | |
: super(hintText: hintText, suffix: suffixIcon); | |
} | |
class PasswordField1 extends StatefulWidget { | |
const PasswordField1({this.hintText, this.decoration}); | |
final String hintText; | |
final InputDecoration decoration; | |
@override | |
_PasswordField1State createState() => new _PasswordField1State(); | |
} | |
class _PasswordField1State extends State<PasswordField1> { | |
bool _obscureText = true; | |
@override | |
Widget build(BuildContext context) { | |
return TextFormField( | |
obscureText: _obscureText, | |
decoration: SignInDecoration( | |
context, | |
hintText: 'Password', | |
suffixIcon: GestureDetector( | |
onTap: () { | |
setState( | |
() { | |
_obscureText = !_obscureText; | |
}, | |
); | |
}, | |
child: Icon(_obscureText ? Icons.visibility : Icons.visibility_off), | |
), | |
), | |
); | |
} | |
} | |
class PasswordField2 extends StatefulWidget { | |
const PasswordField2({this.hintText, this.decoration}); | |
final String hintText; | |
final InputDecoration decoration; | |
@override | |
_PasswordField2State createState() => new _PasswordField2State(); | |
} | |
class _PasswordField2State extends State<PasswordField2> { | |
bool _obscureText = true; | |
@override | |
Widget build(BuildContext context) { | |
return TextFormField( | |
obscureText: _obscureText, | |
decoration: InputDecoration( | |
hintText: 'Password', | |
suffixIcon: GestureDetector( | |
onTap: () { | |
setState( | |
() { | |
_obscureText = !_obscureText; | |
}, | |
); | |
}, | |
child: Icon(_obscureText ? Icons.visibility : Icons.visibility_off), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment