From 1d906e7b7ca170a4383706205da9d6df237e57df Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 17 Mar 2023 04:16:14 +0530 Subject: [PATCH] placeholder files --- .../home_page/editor_pane/url_card.dart | 2 +- lib/widgets/code_generator.dart | 15 ++ lib/widgets/code_highlighter.dart | 189 ++++++++++++++++++ pubspec.lock | 18 +- pubspec.yaml | 4 +- 5 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 lib/widgets/code_generator.dart create mode 100644 lib/widgets/code_highlighter.dart diff --git a/lib/screens/home_page/editor_pane/url_card.dart b/lib/screens/home_page/editor_pane/url_card.dart index 46d1a1b1..af82cd2b 100644 --- a/lib/screens/home_page/editor_pane/url_card.dart +++ b/lib/screens/home_page/editor_pane/url_card.dart @@ -183,7 +183,7 @@ class _SendRequestButtonState extends ConsumerState { .read(sentRequestIdStateProvider.notifier) .update((state) => activeId); await Future.delayed( - const Duration(seconds: 3), + const Duration(seconds: 1), () async { await ref .read(collectionStateNotifierProvider.notifier) diff --git a/lib/widgets/code_generator.dart b/lib/widgets/code_generator.dart new file mode 100644 index 00000000..7f1642b6 --- /dev/null +++ b/lib/widgets/code_generator.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; + +class CodeGenerator extends StatefulWidget { + const CodeGenerator({super.key}); + + @override + State createState() => _CodeGeneratorState(); +} + +class _CodeGeneratorState extends State { + @override + Widget build(BuildContext context) { + return Container(); + } +} diff --git a/lib/widgets/code_highlighter.dart b/lib/widgets/code_highlighter.dart new file mode 100644 index 00000000..96018c60 --- /dev/null +++ b/lib/widgets/code_highlighter.dart @@ -0,0 +1,189 @@ +import 'package:flutter/material.dart'; +import 'package:highlighter/highlighter.dart' show highlight, Node; +import 'package:flutter_highlighter/themes/xcode.dart'; + +class CodeHighlight extends StatelessWidget { + late final String source; + + /// [All available languages](https://github.com/predatorx7/highlight/tree/master/highlight/lib/languages) + final String? language; + + /// [All available themes](https://github.com/predatorx7/highlight/blob/master/flutter_highlighter/lib/themes) + final Map theme; + + /// Padding + final EdgeInsetsGeometry padding; + + /// Specify text styles such as font family and font size + final TextStyle? textStyle; + + CodeHighlight({ + super.key, + required String input, + this.language, + this.textStyle, + this.padding = const EdgeInsets.all(12), + this.theme = xcodeTheme, + int tabSize = 4, // TODO: https://github.com/flutter/flutter/issues/50087 + }) { + if (input.startsWith("\t")) { + source = input.replaceAll('\t', ' ' * tabSize); + } else { + source = input; + } + } + + List _convert(List nodes) { + List spans = []; + var currentSpans = spans; + List> stack = []; + + _traverse(Node node) { + if (node.value != null) { + currentSpans.add(node.className == null + ? TextSpan(text: node.value) + : TextSpan(text: node.value, style: theme[node.className!])); + } else if (node.children != null) { + List tmp = []; + currentSpans + .add(TextSpan(children: tmp, style: theme[node.className!])); + stack.add(currentSpans); + currentSpans = tmp; + + node.children!.forEach((n) { + _traverse(n); + if (n == node.children!.last) { + currentSpans = stack.isEmpty ? spans : stack.removeLast(); + } + }); + } + } + + for (var node in nodes) { + _traverse(node); + } + + return spans; + } + + static const _rootKey = 'root'; + static const _defaultFontColor = Color(0xff000000); + static const _defaultBackgroundColor = Color(0xffffffff); + + // TODO: dart:io is not available at web platform currently + // See: https://github.com/flutter/flutter/issues/39998 + // So we just use monospace here for now + static const _defaultFontFamily = 'monospace'; + + @override + Widget build(BuildContext context) { + var _textStyle = TextStyle( + fontFamily: _defaultFontFamily, + color: theme[_rootKey]?.color ?? _defaultFontColor, + ); + if (textStyle != null) { + _textStyle = _textStyle.merge(textStyle); + } + + return Padding( + //color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor, + padding: padding, + child: SelectableText.rich( + TextSpan( + style: _textStyle, + children: + _convert(highlight.parse(source, language: language).nodes!), + ), + ), + ); + } +} + +/* +CodeHighlight( + input: encoder.convert(requestHeaders), + language: 'json', + textStyle: kCodeStyle, + ) +*/ + +const code = + '''// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:flutter/material.dart'; + +void main() => runApp(MyApp()); + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + debugShowCheckedModeBanner: false, + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: const MyHomePage(title: 'Flutter Demo Home Page'), + ); + } +} + +class MyHomePage extends StatefulWidget { + final String title; + + const MyHomePage({ + Key? key, + required this.title, + }) : super(key: key); + + @override + State createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + int _counter = 0; + + void _incrementCounter() { + setState(() { + _counter++; + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(widget.title), + ), + body: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text( + 'You have pushed the button this many times:', + ), + Text( + '\$_counter', + style: Theme.of(context).textTheme.headlineMedium, + ), + ], + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: _incrementCounter, + tooltip: 'Increment', + child: const Icon(Icons.add), + ), + ); + } +} +'''; + +const code2 = '''void main() { + for (var i = 0; i < 4; i++) { + print('hello \$i'); + } +} +'''; diff --git a/pubspec.lock b/pubspec.lock index 3d576fd1..b1211930 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -102,14 +102,14 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_json_view: + flutter_highlighter: dependency: "direct main" description: - name: flutter_json_view - sha256: "4adc84e96c8bbd1ee874c5b8a3f6a404edccab9757a12edcde4c45c382b95214" + name: flutter_highlighter + sha256: "93173afd47a9ada53f3176371755e7ea4a1065362763976d06d6adfb4d946e10" url: "https://pub.dev" source: hosted - version: "1.0.0" + version: "0.1.1" flutter_lints: dependency: "direct dev" description: @@ -139,6 +139,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.3" + highlighter: + dependency: "direct main" + description: + name: highlighter + sha256: "92180c72b9da8758e1acf39a45aa305a97dcfe2fdc8f3d1d2947c23f2772bfbc" + url: "https://pub.dev" + source: hosted + version: "0.1.1" http: dependency: "direct main" description: @@ -148,7 +156,7 @@ packages: source: hosted version: "0.13.5" http_parser: - dependency: transitive + dependency: "direct main" description: name: http_parser sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" diff --git a/pubspec.yaml b/pubspec.yaml index bf484183..24f08ab6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,9 +15,11 @@ dependencies: uuid: ^3.0.7 davi: ^3.2.0 http: ^0.13.5 + http_parser: ^4.0.2 collection: ^1.17.0 - flutter_json_view: ^1.0.0 google_fonts: ^4.0.3 + flutter_highlighter: ^0.1.1 + highlighter: ^0.1.1 dev_dependencies: flutter_test: