Skip to content

Instantly share code, notes, and snippets.

View diamantidis's full-sized avatar

Ioannis Diamantidis diamantidis

View GitHub Profile
@diamantidis
diamantidis / ContentView.swift
Created October 30, 2021 10:03
ContentView.swift for medium post
@State var selectedIndex: Int? = nil
let options: [String] = ["GraphQL", "Swift", "Vapor"]
var body: some View {
// ...
PickerField("Select an option", data: self.options, selectionIndex: self.$selectedIndex)
// ...
}
@diamantidis
diamantidis / PickerField.swift
Created October 30, 2021 10:01
PickerField for medium post
import SwiftUI
struct PickerField: UIViewRepresentable {
// MARK: - Public properties
@Binding var selectionIndex: Int?
// MARK: - Initializers
init<S>(_ title: S, data: [String], selectionIndex: Binding<Int?>) where S: StringProtocol {
self.placeholder = String(title)
self.data = data
@diamantidis
diamantidis / PickerTextField.swift
Created October 30, 2021 10:00
PickerTextField for medium post
import SwiftUI
class PickerTextField: UITextField {
// MARK: - Public properties
var data: [String]
@Binding var selectionIndex: Int?
// MARK: - Initializers
init(data: [String], selectionIndex: Binding<Int?>) {
self.data = data
@diamantidis
diamantidis / index.html
Created October 17, 2021 17:32
Event handler for toggle (for Medium post)
<script type="text/javascript">
var _selector = document.querySelector('input[name=myCheckbox]');
_selector.addEventListener('change', function(event) {
var message = (_selector.checked) ? "Toggle Switch is on" : "Toggle Switch is off";
if (messageHandler) {
messageHandler.postMessage(message);
}
});
</script>
@diamantidis
diamantidis / WebView.dart
Created October 17, 2021 17:31
Flutter web view javascriptChannels (for Medium post)
Widget buildWebView() {
return WebView(
.
.
.
javascriptChannels: <JavascriptChannel>{
JavascriptChannel(
name: 'messageHandler',
onMessageReceived: (JavascriptMessage message) {
print("message from the web view=\"${message.message}\"");
@diamantidis
diamantidis / WebView.dart
Created October 17, 2021 17:29
Flutter WebView
WebViewController? _webViewController;
Widget buildWebView() {
return WebView(
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) async {
_webViewController = webViewController;
String fileContent = await rootBundle.loadString('assets/index.html');
_webViewController?.loadUrl(Uri.dataFromString(fileContent, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString());
@diamantidis
diamantidis / index.html
Last active October 17, 2021 17:29
HTML page with a title and a toggle switch
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<style>
.switch { position: relative; display: inline-block; width: 60px; height: 34px; }
.switch input { opacity: 0; width: 0; height: 0; }
label.switch { outline: none; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; outline: none; }
.slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; }
class ArticlePage extends StatelessWidget {
final List<Section> sections;
const ArticlePage({Key? key, required this.sections}) : super(key: key);
@override
Widget build(BuildContext context) {
final tableOfContents = TableOfContents(
sections: sections,
onItemTap: (section) {
class TableOfContents extends StatelessWidget {
final List<Section> sections;
final void Function(Section) onItemTap;
const TableOfContents({
Key? key,
this.sections = const <Section>[],
required this.onItemTap,
}) : super(key: key);