|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter/services.dart'; |
| 5 | +import 'package:fimber/fimber.dart'; |
| 6 | + |
| 7 | +import 'package:git_bindings/git_bindings.dart'; |
| 8 | +import 'package:gitjournal/screens/githostsetup_sshkey.dart'; |
| 9 | +import 'package:gitjournal/utils.dart'; |
| 10 | + |
| 11 | +class GitRemoteSettingsScreen extends StatefulWidget { |
| 12 | + @override |
| 13 | + _GitRemoteSettingsScreenState createState() => |
| 14 | + _GitRemoteSettingsScreenState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _GitRemoteSettingsScreenState extends State<GitRemoteSettingsScreen> { |
| 18 | + String publicKey = ""; |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + getSSHPublicKey().then((String val) { |
| 24 | + setState(() { |
| 25 | + publicKey = val; |
| 26 | + }); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + var textTheme = Theme.of(context).textTheme; |
| 33 | + |
| 34 | + var body = Column( |
| 35 | + children: <Widget>[ |
| 36 | + Text( |
| 37 | + "SSH Public Key -", |
| 38 | + style: textTheme.body2, |
| 39 | + textAlign: TextAlign.left, |
| 40 | + ), |
| 41 | + const SizedBox(height: 16.0), |
| 42 | + PublicKeyWidget(publicKey), |
| 43 | + const SizedBox(height: 16.0), |
| 44 | + const Divider(), |
| 45 | + Button( |
| 46 | + text: "Copy Key", |
| 47 | + onPressed: () => _copyKeyToClipboard(context), |
| 48 | + ), |
| 49 | + Button( |
| 50 | + text: "Regnerate Key", |
| 51 | + onPressed: () => _generateSshKey(context), |
| 52 | + ), |
| 53 | + ], |
| 54 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 55 | + ); |
| 56 | + |
| 57 | + return Scaffold( |
| 58 | + appBar: AppBar( |
| 59 | + title: const Text('Git Remote Settings'), |
| 60 | + leading: IconButton( |
| 61 | + icon: const Icon(Icons.arrow_back), |
| 62 | + onPressed: () { |
| 63 | + Navigator.of(context).pop(); |
| 64 | + }, |
| 65 | + ), |
| 66 | + ), |
| 67 | + body: Padding( |
| 68 | + padding: const EdgeInsets.all(16.0), |
| 69 | + child: body, |
| 70 | + ), |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + void _copyKeyToClipboard(BuildContext context) { |
| 75 | + Clipboard.setData(ClipboardData(text: publicKey)); |
| 76 | + showSnackbar(context, "Public Key copied to Clipboard"); |
| 77 | + } |
| 78 | + |
| 79 | + void _generateSshKey(BuildContext context) { |
| 80 | + var comment = "GitJournal " + |
| 81 | + Platform.operatingSystem + |
| 82 | + " " + |
| 83 | + DateTime.now().toIso8601String().substring(0, 10); // only the date |
| 84 | + |
| 85 | + generateSSHKeys(comment: comment).then((String publicKey) { |
| 86 | + setState(() { |
| 87 | + this.publicKey = publicKey; |
| 88 | + Fimber.d("PublicKey: " + publicKey); |
| 89 | + _copyKeyToClipboard(context); |
| 90 | + }); |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class Button extends StatelessWidget { |
| 96 | + final String text; |
| 97 | + final Function onPressed; |
| 98 | + |
| 99 | + Button({@required this.text, @required this.onPressed}); |
| 100 | + |
| 101 | + @override |
| 102 | + Widget build(BuildContext context) { |
| 103 | + return SizedBox( |
| 104 | + width: double.infinity, |
| 105 | + child: RaisedButton( |
| 106 | + child: Text( |
| 107 | + text, |
| 108 | + textAlign: TextAlign.center, |
| 109 | + style: Theme.of(context).textTheme.button, |
| 110 | + ), |
| 111 | + color: Theme.of(context).primaryColor, |
| 112 | + onPressed: onPressed, |
| 113 | + ), |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments