-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
codelab_rebuild.yaml
1227 lines (1137 loc) · 51.1 KB
/
codelab_rebuild.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: GitHub Desktop Client
steps:
- name: Rebuild window_to_front
steps:
- name: Remove generated code
rmdir: window_to_front
- name: Create window_to_front
flutter: create -t plugin --platforms=linux,macos,windows window_to_front
- name: Update dependencies
path: window_to_front
flutter: pub upgrade --major-versions
- name: Replace analysis_options.yaml
path: window_to_front/analysis_options.yaml
replace-contents: |
include: ../../analysis_options.yaml
- name: Remove example code
path: window_to_front
rmdir: example
- name: Remove the README.md
path: window_to_front
rm: README.md
- name: Remove test
path: window_to_front
rmdir: test
- name: Remove linux/test
path: window_to_front
rmdir: linux/test
- name: Remove windows/test
path: window_to_front
rmdir: windows/test
- name: Patch pubspec.yaml
path: window_to_front/pubspec.yaml
patch: |
--- b/github-client/window_to_front/pubspec.yaml
+++ a/github-client/window_to_front/pubspec.yaml
@@ -1,3 +1,17 @@
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
name: window_to_front
description: "A new Flutter plugin project."
version: 0.0.1
- name: Patch lib/window_to_front.dart
path: window_to_front/lib/window_to_front.dart
patch-u: |
--- b/github-client/window_to_front/lib/window_to_front.dart
+++ a/github-client/window_to_front/lib/window_to_front.dart
@@ -1,8 +1,21 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
import 'window_to_front_platform_interface.dart';
class WindowToFront {
- Future<String?> getPlatformVersion() {
- return WindowToFrontPlatform.instance.getPlatformVersion();
+ static Future<void> activate() {
+ return WindowToFrontPlatform.instance.activate();
}
}
- name: Remove linux/window_to_front_plugin_private.h
rm: window_to_front/linux/window_to_front_plugin_private.h
- name: Patch linux/window_to_front_plugin.cc
path: window_to_front/linux/window_to_front_plugin.cc
patch-u: |
--- b/github-client/window_to_front/linux/window_to_front_plugin.cc
+++ a/github-client/window_to_front/linux/window_to_front_plugin.cc
@@ -1,19 +1,31 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
#include "include/window_to_front/window_to_front_plugin.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
-#include <cstring>
-
-#include "window_to_front_plugin_private.h"
-
#define WINDOW_TO_FRONT_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), window_to_front_plugin_get_type(), \
WindowToFrontPlugin))
struct _WindowToFrontPlugin {
GObject parent_instance;
+
+ FlPluginRegistrar* registrar;
};
G_DEFINE_TYPE(WindowToFrontPlugin, window_to_front_plugin, g_object_get_type())
@@ -26,8 +38,14 @@ static void window_to_front_plugin_handle_method_call(
const gchar* method = fl_method_call_get_name(method_call);
- if (strcmp(method, "getPlatformVersion") == 0) {
- response = get_platform_version();
+ if (strcmp(method, "activate") == 0) {
+ FlView* view = fl_plugin_registrar_get_view(self->registrar);
+ if (view != nullptr) {
+ GtkWindow* window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(view)));
+ gtk_window_present(window);
+ }
+
+ response = FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
@@ -35,14 +53,6 @@ static void window_to_front_plugin_handle_method_call(
fl_method_call_respond(method_call, response, nullptr);
}
-FlMethodResponse* get_platform_version() {
- struct utsname uname_data = {};
- uname(&uname_data);
- g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
- g_autoptr(FlValue) result = fl_value_new_string(version);
- return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
-}
-
static void window_to_front_plugin_dispose(GObject* object) {
G_OBJECT_CLASS(window_to_front_plugin_parent_class)->dispose(object);
}
@@ -63,6 +73,8 @@ void window_to_front_plugin_register_with_registrar(FlPluginRegistrar* registrar
WindowToFrontPlugin* plugin = WINDOW_TO_FRONT_PLUGIN(
g_object_new(window_to_front_plugin_get_type(), nullptr));
+ plugin->registrar = FL_PLUGIN_REGISTRAR(g_object_ref(registrar));
+
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodChannel) channel =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
- name: Patch macos/Classes/WindowToFrontPlugin.swift
path: window_to_front/macos/Classes/WindowToFrontPlugin.swift
patch-u: |
--- b/github-client/window_to_front/macos/Classes/WindowToFrontPlugin.swift
+++ a/github-client/window_to_front/macos/Classes/WindowToFrontPlugin.swift
@@ -1,3 +1,17 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
import Cocoa
import FlutterMacOS
@@ -10,8 +24,9 @@ public class WindowToFrontPlugin: NSObject, FlutterPlugin {
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
- case "getPlatformVersion":
- result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
+ case "activate":
+ NSApplication.shared.activate(ignoringOtherApps: true)
+ result(nil)
default:
result(FlutterMethodNotImplemented)
}
- name: Patch windows/window_to_front_plugin.cpp
path: window_to_front/windows/window_to_front_plugin.cpp
patch-u: |
--- b/github-client/window_to_front/windows/window_to_front_plugin.cpp
+++ a/github-client/window_to_front/windows/window_to_front_plugin.cpp
@@ -1,17 +1,27 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
#include "window_to_front_plugin.h"
// This must be included before many other Windows headers.
#include <windows.h>
-// For getPlatformVersion; remove unless needed for your plugin implementation.
-#include <VersionHelpers.h>
-
#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
#include <memory>
-#include <sstream>
namespace window_to_front {
@@ -23,7 +33,7 @@ void WindowToFrontPlugin::RegisterWithRegistrar(
registrar->messenger(), "window_to_front",
&flutter::StandardMethodCodec::GetInstance());
- auto plugin = std::make_unique<WindowToFrontPlugin>();
+ auto plugin = std::make_unique<WindowToFrontPlugin>(registrar);
channel->SetMethodCallHandler(
[plugin_pointer = plugin.get()](const auto &call, auto result) {
@@ -33,24 +43,29 @@ void WindowToFrontPlugin::RegisterWithRegistrar(
registrar->AddPlugin(std::move(plugin));
}
-WindowToFrontPlugin::WindowToFrontPlugin() {}
+WindowToFrontPlugin::WindowToFrontPlugin(flutter::PluginRegistrarWindows *registrar)
+ : registrar_(registrar) {}
WindowToFrontPlugin::~WindowToFrontPlugin() {}
void WindowToFrontPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
- if (method_call.method_name().compare("getPlatformVersion") == 0) {
- std::ostringstream version_stream;
- version_stream << "Windows ";
- if (IsWindows10OrGreater()) {
- version_stream << "10+";
- } else if (IsWindows8OrGreater()) {
- version_stream << "8";
- } else if (IsWindows7OrGreater()) {
- version_stream << "7";
- }
- result->Success(flutter::EncodableValue(version_stream.str()));
+ if (method_call.method_name().compare("activate") == 0) {
+ // See https://stackoverflow.com/a/34414846/2142626 for an explanation of how
+ // this raises a window to the foreground.
+ HWND m_hWnd = registrar_->GetView()->GetNativeWindow();
+ HWND hCurWnd = ::GetForegroundWindow();
+ DWORD dwMyID = ::GetCurrentThreadId();
+ DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
+ ::AttachThreadInput(dwCurID, dwMyID, TRUE);
+ ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
+ ::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
+ ::SetForegroundWindow(m_hWnd);
+ ::SetFocus(m_hWnd);
+ ::SetActiveWindow(m_hWnd);
+ ::AttachThreadInput(dwCurID, dwMyID, FALSE);
+ result->Success();
} else {
result->NotImplemented();
}
- name: Patch lib/window_to_front_method_channel.dart
path: window_to_front/lib/window_to_front_method_channel.dart
patch-u: |
--- b/github-client/window_to_front/lib/window_to_front_method_channel.dart
+++ a/github-client/window_to_front/lib/window_to_front_method_channel.dart
@@ -1,3 +1,17 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
@@ -10,8 +24,7 @@ class MethodChannelWindowToFront extends WindowToFrontPlatform {
final methodChannel = const MethodChannel('window_to_front');
@override
- Future<String?> getPlatformVersion() async {
- final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
- return version;
+ Future<void> activate() async {
+ return methodChannel.invokeMethod('activate');
}
}
- name: Patch lib/window_to_front_platform_interface.dart
path: window_to_front/lib/window_to_front_platform_interface.dart
patch-u: |
--- b/github-client/window_to_front/lib/window_to_front_platform_interface.dart
+++ a/github-client/window_to_front/lib/window_to_front_platform_interface.dart
@@ -1,3 +1,17 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'window_to_front_method_channel.dart';
@@ -23,7 +37,7 @@ abstract class WindowToFrontPlatform extends PlatformInterface {
_instance = instance;
}
- Future<String?> getPlatformVersion() {
- throw UnimplementedError('platformVersion() has not been implemented.');
+ Future<void> activate() {
+ throw UnimplementedError('activate() has not been implemented.');
}
}
- name: Patch windows/window_to_front_plugin.h
path: window_to_front/windows/window_to_front_plugin.h
patch-u: |
--- b/github-client/window_to_front/windows/window_to_front_plugin.h
+++ a/github-client/window_to_front/windows/window_to_front_plugin.h
@@ -1,3 +1,17 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
#ifndef FLUTTER_PLUGIN_WINDOW_TO_FRONT_PLUGIN_H_
#define FLUTTER_PLUGIN_WINDOW_TO_FRONT_PLUGIN_H_
@@ -12,7 +26,7 @@ class WindowToFrontPlugin : public flutter::Plugin {
public:
static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar);
- WindowToFrontPlugin();
+ WindowToFrontPlugin(flutter::PluginRegistrarWindows *registrar);
virtual ~WindowToFrontPlugin();
@@ -24,6 +38,9 @@ class WindowToFrontPlugin : public flutter::Plugin {
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
+
+ // The registrar for this plugin, for accessing the window.
+ flutter::PluginRegistrarWindows *registrar_;
};
} // namespace window_to_front
- name: step_03
steps:
- name: Remove generated code
rmdir: step_03
- name: Create project
flutter: create github_client --platforms windows,macos,linux
- name: Update dependencies
path: github_client
flutter: pub upgrade --major-versions
- name: Configure analysis_options.yaml
path: github_client/analysis_options.yaml
replace-contents: |
include: ../../analysis_options.yaml
- name: Remove the README.md.
rm: github_client/README.md
- name: VSCode config
mkdir: github_client/.vscode
- name: Add launch.json
path: github_client/.vscode/launch.json
replace-contents: |
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "github_client",
"request": "launch",
"type": "dart"
}
]
}
- name: Patch lib/main.dart
path: github_client/lib/main.dart
patch-u: |
--- b/github-client/step_03/lib/main.dart
+++ a/github-client/step_03/lib/main.dart
@@ -1,3 +1,17 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
import 'package:flutter/material.dart';
void main() {
- name: Patch test/widget_test.dart
path: github_client/test/widget_test.dart
patch-u: |
--- b/github-client/step_03/test/widget_test.dart
+++ a/github-client/step_03/test/widget_test.dart
@@ -11,7 +11,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:github_client/main.dart';
void main() {
- testWidgets('Counter increments smoke test', (WidgetTester tester) async {
+ testWidgets('Counter increments smoke test', (tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
- name: Copy step_03
copydir:
from: github_client
to: step_03
- name: step_04
steps:
- name: Remove generated code
rmdir: step_04
- name: Add http oauth2 url_launcher
path: github_client
flutter: pub add http oauth2 url_launcher
- name: Replace lib/main.dart
path: github_client/lib/main.dart
replace-contents: |
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'package:flutter/material.dart';
import 'github_oauth_credentials.dart';
import 'src/github_login.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'GitHub Client',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
visualDensity: VisualDensity.adaptivePlatformDensity,
useMaterial3: true,
),
home: const MyHomePage(title: 'GitHub Client'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return GithubLoginWidget(
builder: (context, httpClient) {
return Scaffold(
appBar: AppBar(
title: Text(title),
elevation: 2,
),
body: const Center(
child: Text(
'You are logged in to GitHub!',
),
),
);
},
githubClientId: githubClientId,
githubClientSecret: githubClientSecret,
githubScopes: githubScopes,
);
}
}
- name: Create lib/github_oauth_credentials.dart
path: github_client/lib/github_oauth_credentials.dart
replace-contents: |
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO(CodelabUser): Create an OAuth App
const githubClientId = 'YOUR_GITHUB_CLIENT_ID_HERE';
const githubClientSecret = 'YOUR_GITHUB_CLIENT_SECRET_HERE';
// OAuth scopes for repository and user information
const githubScopes = ['repo', 'read:org'];
- name: Make lib/src directory
path: github_client/lib
mkdir: src
- name: Create lib/src/github_login.dart
path: github_client/lib/src/github_login.dart
replace-contents: |
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:url_launcher/url_launcher.dart';
final _authorizationEndpoint =
Uri.parse('https://github.com/login/oauth/authorize');
final _tokenEndpoint = Uri.parse('https://github.com/login/oauth/access_token');
class GithubLoginWidget extends StatefulWidget {
const GithubLoginWidget({
required this.builder,
required this.githubClientId,
required this.githubClientSecret,
required this.githubScopes,
super.key,
});
final AuthenticatedBuilder builder;
final String githubClientId;
final String githubClientSecret;
final List<String> githubScopes;
@override
State<GithubLoginWidget> createState() => _GithubLoginState();
}
typedef AuthenticatedBuilder = Widget Function(
BuildContext context, oauth2.Client client);
class _GithubLoginState extends State<GithubLoginWidget> {
HttpServer? _redirectServer;
oauth2.Client? _client;
@override
Widget build(BuildContext context) {
final client = _client;
if (client != null) {
return widget.builder(context, client);
}
return Scaffold(
appBar: AppBar(
title: const Text('Github Login'),
elevation: 2,
),
body: Center(
child: ElevatedButton(
onPressed: () async {
await _redirectServer?.close();
// Bind to an ephemeral port on localhost
_redirectServer = await HttpServer.bind('localhost', 0);
var authenticatedHttpClient = await _getOAuth2Client(
Uri.parse('http://localhost:${_redirectServer!.port}/auth'));
setState(() {
_client = authenticatedHttpClient;
});
},
child: const Text('Login to Github'),
),
),
);
}
Future<oauth2.Client> _getOAuth2Client(Uri redirectUrl) async {
if (widget.githubClientId.isEmpty || widget.githubClientSecret.isEmpty) {
throw const GithubLoginException(
'githubClientId and githubClientSecret must be not empty. '
'See `lib/github_oauth_credentials.dart` for more detail.');
}
var grant = oauth2.AuthorizationCodeGrant(
widget.githubClientId,
_authorizationEndpoint,
_tokenEndpoint,
secret: widget.githubClientSecret,
httpClient: _JsonAcceptingHttpClient(),
);
var authorizationUrl =
grant.getAuthorizationUrl(redirectUrl, scopes: widget.githubScopes);
await _redirect(authorizationUrl);
var responseQueryParameters = await _listen();
var client =
await grant.handleAuthorizationResponse(responseQueryParameters);
return client;
}
Future<void> _redirect(Uri authorizationUrl) async {
if (await canLaunchUrl(authorizationUrl)) {
await launchUrl(authorizationUrl);
} else {
throw GithubLoginException('Could not launch $authorizationUrl');
}
}
Future<Map<String, String>> _listen() async {
var request = await _redirectServer!.first;
var params = request.uri.queryParameters;
request.response.statusCode = 200;
request.response.headers.set('content-type', 'text/plain');
request.response.writeln('Authenticated! You can close this tab.');
await request.response.close();
await _redirectServer!.close();
_redirectServer = null;
return params;
}
}
class _JsonAcceptingHttpClient extends http.BaseClient {
final _httpClient = http.Client();
@override
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers['Accept'] = 'application/json';
return _httpClient.send(request);
}
}
class GithubLoginException implements Exception {
const GithubLoginException(this.message);
final String message;
@override
String toString() => message;
}
- name: Patch macos/Runner/DebugProfile.entitlements
path: github_client/macos/Runner/DebugProfile.entitlements
patch: |
10a11,12
> <key>com.apple.security.network.client</key>
> <true/>
- name: Patch macos/Runner/Release.entitlements
path: github_client/macos/Runner/Release.entitlements
patch: |
6a7,10
> <key>com.apple.security.network.server</key>
> <true/>
> <key>com.apple.security.network.client</key>
> <true/>
- name: Replace test/widget_test.dart
path: github_client/test/widget_test.dart
replace-contents: |
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.
import 'package:flutter_test/flutter_test.dart';
import 'package:github_client/main.dart';
void main() {
testWidgets('Counter increments smoke test', (tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
// Verify that our counter starts at 0.
expect(find.text('Github Login'), findsOneWidget);
expect(find.text('Not this text'), findsNothing);
});
}
- name: Build macOS app
platforms: [ macos ]
path: github_client
flutter: build macos
- name: Build linux app
platforms: [ linux ]
path: github_client
flutter: build linux
- name: Build windows app
platforms: [ windows ]
path: github_client
flutter: build windows
- name: Copy step_04
copydir:
from: github_client
to: step_04
- name: step_05
steps:
- name: Remove generated code
rmdir: step_05
- name: Add dependencies
path: github_client
flutter: pub add github
- name: Patch lib/main.dart
path: github_client/lib/main.dart
patch-u: |
--- b/github-client/step_05/lib/main.dart
+++ a/github-client/step_05/lib/main.dart
@@ -13,6 +13,8 @@
// limitations under the License.
import 'package:flutter/material.dart';
+import 'package:github/github.dart';
+
import 'github_oauth_credentials.dart';
import 'src/github_login.dart';
@@ -45,16 +47,23 @@ class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
return GithubLoginWidget(
builder: (context, httpClient) {
- return Scaffold(
- appBar: AppBar(
- title: Text(title),
- elevation: 2,
- ),
- body: const Center(
- child: Text(
- 'You are logged in to GitHub!',
- ),
- ),
+ return FutureBuilder<CurrentUser>(
+ future: viewerDetail(httpClient.credentials.accessToken),
+ builder: (context, snapshot) {
+ return Scaffold(
+ appBar: AppBar(
+ title: Text(title),
+ elevation: 2,
+ ),
+ body: Center(
+ child: Text(
+ snapshot.hasData
+ ? 'Hello ${snapshot.data!.login}!'
+ : 'Retrieving viewer login details...',
+ ),
+ ),
+ );
+ },
);
},
githubClientId: githubClientId,
@@ -63,3 +72,8 @@ class MyHomePage extends StatelessWidget {
);
}
}
+
+Future<CurrentUser> viewerDetail(String accessToken) async {
+ final gitHub = GitHub(auth: Authentication.withToken(accessToken));
+ return gitHub.users.getCurrentUser();
+}
- name: Copy step_05
copydir:
from: github_client
to: step_05
- name: step_06
steps:
- name: Remove generated code
rmdir: step_06
- name: Add dependencies
path: github_client
flutter: pub add --path ../window_to_front window_to_front
- name: Patch lib/main.dart
path: github_client/lib/main.dart
patch-u: |
--- b/github-client/step_06/lib/main.dart
+++ a/github-client/step_06/lib/main.dart
@@ -14,6 +14,7 @@
import 'package:flutter/material.dart';
import 'package:github/github.dart';
+import 'package:window_to_front/window_to_front.dart';
import 'github_oauth_credentials.dart';
import 'src/github_login.dart';
@@ -47,6 +48,7 @@ class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
return GithubLoginWidget(
builder: (context, httpClient) {
+ WindowToFront.activate();
return FutureBuilder<CurrentUser>(
future: viewerDetail(httpClient.credentials.accessToken),
builder: (context, snapshot) {
- name: Build macOS app
platforms: [ macos ]
path: github_client
flutter: build macos
- name: Build linux app
platforms: [ linux ]
path: github_client
flutter: build linux
- name: Build windows app
platforms: [ windows ]
path: github_client
flutter: build windows
- name: Copy step_06
copydir:
from: github_client
to: step_06
- name: step_07
steps:
- name: Remove generated code
rmdir: step_07
- name: Add dependencies
path: github_client
flutter: pub add fluttericon
- name: Add lib/src/github_summary.dart
path: github_client/lib/src/github_summary.dart
replace-contents: |
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'package:flutter/material.dart';
import 'package:fluttericon/octicons_icons.dart';
import 'package:github/github.dart';
import 'package:url_launcher/url_launcher_string.dart';
class GitHubSummary extends StatefulWidget {
const GitHubSummary({required this.gitHub, super.key});
final GitHub gitHub;
@override
State<GitHubSummary> createState() => _GitHubSummaryState();
}
class _GitHubSummaryState extends State<GitHubSummary> {
int _selectedIndex = 0;
@override
Widget build(BuildContext context) {
return Row(
children: [
NavigationRail(
selectedIndex: _selectedIndex,
onDestinationSelected: (index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.selected,
destinations: const [
NavigationRailDestination(
icon: Icon(Octicons.repo),
label: Text('Repositories'),
),
NavigationRailDestination(
icon: Icon(Octicons.issue_opened),
label: Text('Assigned Issues'),
),
NavigationRailDestination(
icon: Icon(Octicons.git_pull_request),
label: Text('Pull Requests'),
),
],
),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: IndexedStack(
index: _selectedIndex,
children: [
RepositoriesList(gitHub: widget.gitHub),
AssignedIssuesList(gitHub: widget.gitHub),
PullRequestsList(gitHub: widget.gitHub),
],
),
),
],
);
}
}
class RepositoriesList extends StatefulWidget {
const RepositoriesList({required this.gitHub, super.key});
final GitHub gitHub;
@override
State<RepositoriesList> createState() => _RepositoriesListState();
}
class _RepositoriesListState extends State<RepositoriesList> {
@override
initState() {
super.initState();
_repositories = widget.gitHub.repositories.listRepositories().toList();
}
late Future<List<Repository>> _repositories;
@override
Widget build(BuildContext context) {
return FutureBuilder<List<Repository>>(
future: _repositories,
builder: (context, snapshot) {
if (snapshot.hasError) {