From 12e16f83e5259210a70165dcdb72d230a2010d03 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 19 Feb 2020 18:06:59 +0100 Subject: [PATCH] KeyEditors: Remove code duplication --- lib/setup/key_editors.dart | 65 ++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/lib/setup/key_editors.dart b/lib/setup/key_editors.dart index cadd417f..bdc44fdf 100644 --- a/lib/setup/key_editors.dart +++ b/lib/setup/key_editors.dart @@ -7,31 +7,14 @@ class PublicKeyEditor extends StatelessWidget { @override Widget build(BuildContext context) { - return SizedBox( - width: double.infinity, - height: 80.0, - child: Container( - color: Theme.of(context).buttonColor, - child: SingleChildScrollView( - child: Container( - padding: const EdgeInsets.all(8.0), - child: TextFormField( - controller: controller, - textAlign: TextAlign.left, - maxLines: null, - style: Theme.of(context).textTheme.body1, - autovalidate: true, - validator: (String val) { - if (!val.startsWith("ssh-")) { - return "Invalid Public Key"; - } - return ""; - }, - ), - ), - ), - ), - ); + return KeyEditor(controller, _validator); + } + + String _validator(String val) { + if (!val.startsWith("ssh-")) { + return "Invalid Public Key"; + } + return ""; } } @@ -40,6 +23,28 @@ class PrivateKeyEditor extends StatelessWidget { PrivateKeyEditor(this.controller); + @override + Widget build(BuildContext context) { + return KeyEditor(controller, _validator); + } + + String _validator(String val) { + if (!val.startsWith("-----BEGIN RSA PRIVATE KEY-----")) { + return "Invalid Private Key"; + } + if (!val.startsWith("-----END RSA PRIVATE KEY-----")) { + return "Invalid Private Key"; + } + return ""; + } +} + +class KeyEditor extends StatelessWidget { + final TextEditingController controller; + final Function validator; + + KeyEditor(this.controller, this.validator); + @override Widget build(BuildContext context) { return SizedBox( @@ -56,15 +61,7 @@ class PrivateKeyEditor extends StatelessWidget { maxLines: null, style: Theme.of(context).textTheme.body1, autovalidate: true, - validator: (String val) { - if (!val.startsWith("-----BEGIN RSA PRIVATE KEY-----")) { - return "Invalid Private Key"; - } - if (!val.startsWith("-----END RSA PRIVATE KEY-----")) { - return "Invalid Private Key"; - } - return ""; - }, + validator: validator, ), ), ),