mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 10:17:16 +08:00
@ -1,53 +1,64 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PublicKeyEditor extends StatelessWidget {
|
||||
final Function valueChanged;
|
||||
final Key formKey;
|
||||
final TextEditingController _controller;
|
||||
|
||||
PublicKeyEditor(this.valueChanged);
|
||||
PublicKeyEditor(this.formKey, this._controller);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KeyEditor(valueChanged, _validator);
|
||||
return KeyEditor(formKey, _controller, _validator);
|
||||
}
|
||||
|
||||
String _validator(String val) {
|
||||
if (!val.startsWith("ssh-")) {
|
||||
return "Invalid Public Key";
|
||||
val = val.trim();
|
||||
if (!val.startsWith("ssh-rsa ")) {
|
||||
return "Invalid Public Key - Doesn't start with ssh-rsa";
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class PrivateKeyEditor extends StatelessWidget {
|
||||
final Function valueChanged;
|
||||
final Key formKey;
|
||||
final TextEditingController _controller;
|
||||
|
||||
PrivateKeyEditor(this.valueChanged);
|
||||
PrivateKeyEditor(this.formKey, this._controller);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return KeyEditor(valueChanged, _validator);
|
||||
return KeyEditor(formKey, _controller, _validator);
|
||||
}
|
||||
|
||||
String _validator(String val) {
|
||||
if (!val.startsWith("-----BEGIN RSA PRIVATE KEY-----")) {
|
||||
val = val.trim();
|
||||
if (!val.startsWith("-----BEGIN ")) {
|
||||
return "Invalid Private Key";
|
||||
}
|
||||
if (!val.startsWith("-----END RSA PRIVATE KEY-----")) {
|
||||
if (!val.endsWith("PRIVATE KEY-----")) {
|
||||
return "Invalid Private Key";
|
||||
}
|
||||
return "";
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class KeyEditor extends StatelessWidget {
|
||||
final Function valueChanged;
|
||||
final Key formKey;
|
||||
final TextEditingController textEditingController;
|
||||
final Function validator;
|
||||
|
||||
KeyEditor(this.valueChanged, this.validator);
|
||||
KeyEditor(this.formKey, this.textEditingController, this.validator) {
|
||||
assert(formKey != null);
|
||||
assert(textEditingController != null);
|
||||
assert(validator != null);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var form = Form(
|
||||
key: formKey,
|
||||
child: Builder(builder: (context) {
|
||||
return TextFormField(
|
||||
textAlign: TextAlign.left,
|
||||
@ -55,13 +66,7 @@ class KeyEditor extends StatelessWidget {
|
||||
style: Theme.of(context).textTheme.body1,
|
||||
autovalidate: true,
|
||||
validator: validator,
|
||||
onChanged: (String newVal) {
|
||||
if (Form.of(context).validate()) {
|
||||
valueChanged(newVal);
|
||||
} else {
|
||||
valueChanged("");
|
||||
}
|
||||
},
|
||||
controller: textEditingController,
|
||||
);
|
||||
}),
|
||||
autovalidate: true,
|
||||
|
@ -207,14 +207,23 @@ class GitHostUserProvidedKeys extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _GitHostUserProvidedKeysState extends State<GitHostUserProvidedKeys> {
|
||||
String publicKey = "";
|
||||
String privateKey = "";
|
||||
final _publicKeyController = TextEditingController();
|
||||
final _privateKeyController = TextEditingController();
|
||||
GlobalKey<FormState> _publicFormKey;
|
||||
GlobalKey<FormState> _privateFormKey;
|
||||
TextEditingController _publicKeyController;
|
||||
TextEditingController _privateKeyController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_publicFormKey = GlobalKey<FormState>();
|
||||
_privateFormKey = GlobalKey<FormState>();
|
||||
_publicKeyController = TextEditingController();
|
||||
_privateKeyController = TextEditingController();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var nextEnabled = publicKey.isNotEmpty && privateKey.isNotEmpty;
|
||||
return Container(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
@ -223,32 +232,35 @@ class _GitHostUserProvidedKeysState extends State<GitHostUserProvidedKeys> {
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
),
|
||||
const SizedBox(height: 8.0),
|
||||
PublicKeyEditor((String newVal) {
|
||||
setState(() {
|
||||
publicKey = newVal;
|
||||
});
|
||||
}),
|
||||
PublicKeyEditor(_publicFormKey, _publicKeyController),
|
||||
const SizedBox(height: 8.0),
|
||||
Text(
|
||||
"Private Key -",
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
),
|
||||
const SizedBox(height: 8.0),
|
||||
PrivateKeyEditor((String newVal) {
|
||||
setState(() {
|
||||
privateKey = newVal;
|
||||
});
|
||||
}),
|
||||
PrivateKeyEditor(_privateFormKey, _privateKeyController),
|
||||
const SizedBox(height: 16.0),
|
||||
GitHostSetupButton(
|
||||
text: "Next",
|
||||
onPressed: () {
|
||||
if (!nextEnabled) {
|
||||
var publicValid = _publicFormKey.currentState.validate();
|
||||
var privateValid = _privateFormKey.currentState.validate();
|
||||
|
||||
if (!publicValid || !privateValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
var publicKey = _publicKeyController.text.trim();
|
||||
if (!publicKey.endsWith('\n')) {
|
||||
publicKey += '\n';
|
||||
}
|
||||
|
||||
var privateKey = _privateKeyController.text.trim();
|
||||
if (!privateKey.endsWith('\n')) {
|
||||
privateKey += '\n';
|
||||
}
|
||||
|
||||
widget.doneFunction(publicKey, privateKey);
|
||||
},
|
||||
),
|
||||
|
Reference in New Issue
Block a user