From 61271ebe8bbcaaa0e58e86db0fce513c044f5a66 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Sat, 13 May 2023 10:26:18 +0900 Subject: [PATCH] Update README --- README.md | 108 +++++++++++++++++++++++++++++++++++++++++- example/lib/main.dart | 28 ++++++----- 2 files changed, 123 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 528c8ed..18eb1e7 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,110 @@ PasswordTextField is a TextField that manages the toggle of obscure. ## How to use -Replace your `TextField`(`PasswordTextField`) or `TextFormField`(`PasswordTextFormField`). +### `TextField`(`PasswordTextField`) + +```dart +class PasswordWidget extends StatelessWidget { + const PasswordWidget({super.key}); + + @override + Widget build(BuildContext context) { + return Column( + children: [ + Text( + 'TextField', + style: Theme.of(context).textTheme.titleLarge, + ), + const SizedBox( + height: 8, + ), + const PasswordTextField( + decoration: InputDecoration( + border: UnderlineInputBorder(), + hintText: 'underline', + ), + ), + const SizedBox( + height: 16, + ), + const PasswordTextField( + decoration: InputDecoration( + border: OutlineInputBorder(), + hintText: 'outline', + ), + ), + ], + ); + } +} +``` + +### `TextFormField`(`PasswordTextFormField`) + +```dart +class PasswordFormWidget extends StatefulWidget { + const PasswordFormWidget({super.key}); + + @override + State createState() => _PasswordFormWidgetState(); +} + +class _PasswordFormWidgetState extends State { + final _formState = GlobalKey(); + final _textFormFieldController = TextEditingController(); + + @override + void dispose() { + _textFormFieldController.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Form( + key: _formState, + child: Column( + children: [ + Text( + 'TextFormField', + style: Theme.of(context).textTheme.titleLarge, + ), + const SizedBox( + height: 8, + ), + PasswordTextFormField( + controller: _textFormFieldController, + validator: (value) { + if ((value?.length ?? 0) < 6) { + return 'Password must be at least 6 characters'; + } + + return null; + }, + ), + const SizedBox( + height: 16, + ), + OutlinedButton( + onPressed: () { + final state = _formState.currentState; + if (state == null || !state.validate()) { + return; + } + + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Password ${_textFormFieldController.text} is valid!', + ), + ), + ); + }, + child: const Text('Check'), + ), + ], + ), + ); + } +} +``` \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 8a95afb..fce9835 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -18,13 +18,17 @@ class MyApp extends StatelessWidget { title: 'password_text_field demo', theme: ThemeData( useMaterial3: true, - colorSchemeSeed: Colors.blue, - brightness: Brightness.light, + colorScheme: ColorScheme.fromSeed( + seedColor: Colors.blue, + brightness: Brightness.light, + ), ), darkTheme: ThemeData( useMaterial3: true, - colorSchemeSeed: Colors.blue, - brightness: Brightness.dark, + colorScheme: ColorScheme.fromSeed( + seedColor: Colors.blue, + brightness: Brightness.dark, + ), ), home: const MyHomePage(), ); @@ -50,11 +54,11 @@ class MyHomePage extends StatelessWidget { ), child: const Column( children: [ - _PasswordWidget(), + PasswordWidget(), SizedBox( height: 32, ), - _PasswordFormWidget(), + PasswordFormWidget(), ], ), ), @@ -62,8 +66,8 @@ class MyHomePage extends StatelessWidget { } } -class _PasswordWidget extends StatelessWidget { - const _PasswordWidget(); +class PasswordWidget extends StatelessWidget { + const PasswordWidget({super.key}); @override Widget build(BuildContext context) { @@ -96,14 +100,14 @@ class _PasswordWidget extends StatelessWidget { } } -class _PasswordFormWidget extends StatefulWidget { - const _PasswordFormWidget(); +class PasswordFormWidget extends StatefulWidget { + const PasswordFormWidget({super.key}); @override - State<_PasswordFormWidget> createState() => _PasswordFormWidgetState(); + State createState() => _PasswordFormWidgetState(); } -class _PasswordFormWidgetState extends State<_PasswordFormWidget> { +class _PasswordFormWidgetState extends State { final _formState = GlobalKey(); final _textFormFieldController = TextEditingController();