├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example ├── .gitignore ├── .metadata ├── README.md ├── analysis_options.yaml ├── android │ ├── .gitignore │ ├── app │ │ ├── build.gradle │ │ └── src │ │ │ ├── debug │ │ │ └── AndroidManifest.xml │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── kotlin │ │ │ │ └── com │ │ │ │ │ └── example │ │ │ │ │ └── example │ │ │ │ │ └── MainActivity.kt │ │ │ └── res │ │ │ │ ├── drawable │ │ │ │ └── launch_background.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ │ └── values │ │ │ │ └── styles.xml │ │ │ └── profile │ │ │ └── AndroidManifest.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ └── gradle-wrapper.properties │ └── settings.gradle ├── ios │ ├── .gitignore │ ├── Flutter │ │ ├── AppFrameworkInfo.plist │ │ ├── Debug.xcconfig │ │ └── Release.xcconfig │ ├── Podfile │ ├── Podfile.lock │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ │ └── WorkspaceSettings.xcsettings │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ ├── IDEWorkspaceChecks.plist │ │ │ └── WorkspaceSettings.xcsettings │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── Icon-App-1024x1024@1x.png │ │ │ ├── Icon-App-20x20@1x.png │ │ │ ├── Icon-App-20x20@2x.png │ │ │ ├── Icon-App-20x20@3x.png │ │ │ ├── Icon-App-29x29@1x.png │ │ │ ├── Icon-App-29x29@2x.png │ │ │ ├── Icon-App-29x29@3x.png │ │ │ ├── Icon-App-40x40@1x.png │ │ │ ├── Icon-App-40x40@2x.png │ │ │ ├── Icon-App-40x40@3x.png │ │ │ ├── Icon-App-60x60@2x.png │ │ │ ├── Icon-App-60x60@3x.png │ │ │ ├── Icon-App-76x76@1x.png │ │ │ ├── Icon-App-76x76@2x.png │ │ │ └── Icon-App-83.5x83.5@2x.png │ │ └── LaunchImage.imageset │ │ │ ├── Contents.json │ │ │ ├── LaunchImage.png │ │ │ ├── LaunchImage@2x.png │ │ │ ├── LaunchImage@3x.png │ │ │ └── README.md │ │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ │ ├── Info.plist │ │ └── Runner-Bridging-Header.h ├── lib │ ├── dartpad_metadata.yaml │ └── main.dart ├── macos │ ├── .gitignore │ ├── Flutter │ │ ├── Flutter-Debug.xcconfig │ │ ├── Flutter-Release.xcconfig │ │ └── GeneratedPluginRegistrant.swift │ ├── Podfile │ ├── Runner.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── Runner.xcscheme │ ├── Runner.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── Runner │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ ├── Contents.json │ │ │ ├── app_icon_1024.png │ │ │ ├── app_icon_128.png │ │ │ ├── app_icon_16.png │ │ │ ├── app_icon_256.png │ │ │ ├── app_icon_32.png │ │ │ ├── app_icon_512.png │ │ │ └── app_icon_64.png │ │ ├── Base.lproj │ │ └── MainMenu.xib │ │ ├── Configs │ │ ├── AppInfo.xcconfig │ │ ├── Debug.xcconfig │ │ ├── Release.xcconfig │ │ └── Warnings.xcconfig │ │ ├── DebugProfile.entitlements │ │ ├── Info.plist │ │ ├── MainFlutterWindow.swift │ │ └── Release.entitlements ├── pubspec.yaml └── web │ ├── favicon.png │ ├── icons │ ├── Icon-192.png │ └── Icon-512.png │ ├── index.html │ └── manifest.json ├── images ├── dart_code_viewer.gif └── import_example.png ├── lib ├── dart_code_viewer.dart └── src │ ├── dart_code_viewer.dart │ ├── dart_code_viewer_theme.dart │ └── pre_highlighter.dart ├── pubspec.yaml └── test ├── dart_code_viewer_test.dart └── dart_code_viewer_theme_test.dart /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 16 | 1. Go to '...' 17 | 2. Click on '....' 18 | 3. Scroll down to '....' 19 | 4. See error 20 | 21 | **Expected behavior** 22 | A clear and concise description of what you expected to happen. 23 | 24 | **Include the contents of your `dart_code_viewer` directory** 25 | Add contents here. 26 | 27 | **Screenshots** 28 | If applicable, add screenshots to help explain your problem. 29 | 30 | **Desktop (please complete the following information if applicable):** 31 | 32 | - OS: [e.g. iOS] 33 | - Browser [e.g. chrome, safari] 34 | - Version [e.g. 22] 35 | 36 | **Smartphone (please complete the following information if applicable):** 37 | 38 | - Device: [e.g. iPhone6] 39 | - OS: [e.g. iOS8.1] 40 | - Browser [e.g. stock browser, safari] 41 | - Version [e.g. 22] 42 | 43 | **Additional context** 44 | Add any other context about the problem here. 45 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | **/ios/Flutter/.last_build_id 26 | .dart_tool/ 27 | .flutter-plugins 28 | .flutter-plugins-dependencies 29 | .packages 30 | .pub-cache/ 31 | .pub/ 32 | /build/ 33 | 34 | # Web related 35 | lib/generated_plugin_registrant.dart 36 | 37 | # Symbolication related 38 | app.*.symbols 39 | 40 | # Obfuscation related 41 | app.*.map.json 42 | 43 | # Exceptions to above rules. 44 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 45 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | # [0.0.2] - 2020-11-03 3 | 4 | The dart code viewer package has been updated and has been updated to use 5 | not null be default. 6 | 7 | The dart code viewer package is using the elevated button because the elevated button is in the new button Universe. This will make it easier to theme the buttons within the dart code viewer. 8 | 9 | Removed dependency for the Google fonts package. 10 | 11 | # [0.0.1] - 2020-05-22 12 | 13 | The initial release has DartCodeViewer, DartCodeViewerTheme, and DartCodeViewerThemeData. 14 | 15 | The code viewer for the dart language. The code viewer can be used to display dart code. By default the [DartCodeViewer] gives you a Theme based code view. If you are using a [ThemeMode] that is light than you will get the light option. Note that the default background of the code viewer is based off [ColorScheme.background]. 16 | 17 | Supplying a non-null [data] String is required as input. 18 | 19 | Requires one of its ancestors to be a [Material] widget. 20 | 21 | Requires one of its ancestors to be a [MediaQuery] widget. Typically, these are introduced by the [MaterialApp] or [WidgetsApp] widget at the top of your application widget tree. 22 | 23 | String is codified and split into tokens. Depending on the token produced the TextSpan will be a different color. This is based of the [flutter gallery](https://gallery.flutter.dev/). You can find code source [here](https://github.com/flutter/gallery/tree/master/lib/codeviewer) 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to the google_fonts package 2 | 3 | Thank you for your interest in contributing to the `dart_code_viewer` package! We love receiving contributions, and your work helps the whole community. This doc will walk you through the easiest way to make a change and have it submitted to the `dart_code_viewer` package. 4 | 5 | ## Developer workflow 6 | 7 | The easiest workflow for adding a feature/fixing a bug is to test it out on the example app in this 8 | repo. 9 | 10 | ### Environment 11 | 12 | 1. Fork the [dart-code-viewer](https://github.com/JoseAlba/dart-code-viewer) 13 | repo on github. 14 | 1. Clone your fork of the `dart-code-viewer` repo. 15 | 1. Build and run the example app in `example/lib/main.dart` (from the `example/` directory, use `$ flutter run`). 16 | 17 | ### Development 18 | 19 | 1. Make the changes to your local copy of the `dart-code-viewer` package, testing the changes in the example app. 20 | 1. Write a unit test for your change, if possible, in one of the files in `test/`. 21 | 1. Update the `CHANGELOG.md` with a new version number, and a description of the change you're making. 22 | 1. Update the `version:` in the `pubspec.yaml` file to your new version. 23 | 24 | ### Review 25 | 26 | 1. Make sure all the existing tests are passing by using the following command (from the root of the project): `$ flutter test test/`. 27 | 1. Create a PR to merge the branch on your fork into `dart-code-viewer/master`. 28 | 1. Add `JoseAlba` as reviewers on the PR. We will take a look and add any comments. When the PR is ready to be merged, we will merge it and update the package on [pub.dev](https://pub.dev/packages/dart_code_viewer)! 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dart_code_viewer 2 | 3 | The `dart_code_viewer` package for Flutter allows you to easily show and copy dart code in your Flutter application. 4 | 5 | ## Getting Started 6 | 7 | ![Dart Code Viewer Example in light and dark Theme Mode.](images/dart_code_viewer.gif) 8 | 9 | With the `dart_code_viewer` package, you can show Dart code in a Flutter application. The dart_code_viewer is documented well and uses Flutters pattern with Theming. 10 | 11 | The code viewer can be used to display dart code. By default the `DartCodeViewer` gives you a Theme based code view. If you are using a `ThemeMode` that is light than you will get the light option. Note that the default background of the code viewer is based off `ColorScheme.background`. 12 | 13 | The code viewer requieres a non-null `data` String as required input. 14 | 15 | The `DartCodeViewer` requires one of its ancestors to be a `Material` widget. This is because the code viewer uses the `MediaQuery` widget. Which is typically introduced by the [MaterialApp] or [WidgetsApp] widget at the top of your application widget tree. 16 | 17 | To use `dart_code_viewer` first you must add the 'dart_code_viewer' package to your [oubspec dependencies](https://pub.dev/packages/dart_code_viewer#-installing-tab-) 18 | 19 | To import `DartCodeViewer`; 20 | 21 | ```dart 22 | import 'package:dart_code_viewer/dart_code_viewer.dart'; 23 | ``` 24 | 25 | To use `DartCodeViewer` with the default DartCodeViewer: 26 | 27 | ```dart 28 | DartCodeViewer(r''' 29 | void main() { 30 | runApp(MyApp()); 31 | } 32 | ''' 33 | ), 34 | ``` 35 | 36 | `'''` is a handy dart pattern that lets you read multiple string as once instead. Which is really useful if you want to import your dart code as a String like this. 37 | 38 | By simply using the code above you will get the example shown in the GIF. 39 | 40 | You can also customize the theming of the `DartCodeViewer` by using its parameters: 41 | 42 | ```dart 43 | DartCodeViewer(r''' 44 | void main() { 45 | runApp(MyApp()); 46 | } 47 | ''', 48 | backgroundColor: GoogleFonts.robotoMono().copyWith(color: Colors.pink), 49 | ), 50 | ``` 51 | 52 | This will change the background color of the CodeViewer to Pink. 53 | 54 | You can use this [Code viewer tool](https://romannurik.github.io/SlidesCodeHighlighter/) to choose the color for each different highlighted token style. On the left side you put your example code and on the right you can choose the colors you want the code viewer to display. Below is a table that shows you the difference between the code viewer tool and the dart code viewer parameter. 55 | 56 | | Code Viewer Tool | Dart Code Viewer Parameter | 57 | | --- | --- | 58 | | background | backgroundColor | 59 | | plain text | baseStyle | 60 | | Punctuation | punctuationStyle | 61 | | String, values | stringStyle | 62 | | Keywords, tags | keywordStyle | 63 | | Comments | commentStyle | 64 | | Types | classStyle | 65 | | Numbers | numberStyle | 66 | 67 | You can use `DartCodeViewer` other constructor `DartCodeViewer.textColor` to change the color properties instead of the TextStyles. You can also set the TextStyle incase you don't want to use the default textStlye `RobotoMono`. Here is an example on how you can use this constructor: 68 | 69 | ```dart 70 | DartCodeViewer.textColor(r''' 71 | void main() { 72 | runApp(MyApp()); 73 | } 74 | ''', 75 | textStyle: GoogleFonts.lato(), 76 | commentColor: Colors.grey, 77 | baseColor: Colors.pink, 78 | ), 79 | ``` 80 | 81 | In the case above we are using the text style lato and we are changeing the default colors for comment and base color to grey and pink respectively. 82 | 83 | If you are having a hard time figuering out how to choose the Color for this tool. There are a few default DartCodeViewers at your disposal. Here are the code viewers that are well known: 84 | 85 | - `DartCodeViewer.light` 86 | - `DartCodeViewer.lightAlt` 87 | - `DartCodeViewer.dark` 88 | - `DartCodeViewer.darkAlt` 89 | - `DartCodeViewer.designDark` 90 | - `DartCodeViewer.io17` 91 | - `DartCodeViewer.io19` 92 | - `DartCodeViewer.flutterInteract2019` 93 | 94 | Here is an example on how to use these themed code viewers. 95 | 96 | ```dart 97 | DartCodeViewer.designDark(r''' 98 | void main() { 99 | runApp(MyApp()); 100 | } 101 | ''', 102 | ), 103 | ``` 104 | 105 | The DartCodeViewer can also be themed identical to how widgets in the material package get themed. You can theme the code viewer by having an ancestor of `DartCodeViewerTheme`. The dart code viewer theme describes the color, size, and text styles for the dart code viewer it is attached to. 106 | 107 | Descendant widget obtains the current theme's `DartCodeViewerThemeData` object using `DartCodeViewerTheme.of`. When a widget uses `DartCodeViewerTheme.of`, it is automatically rebuilt if the theme later changes. 108 | 109 | Using the `DartCodeViewerThemeData` returns the data from the closest `DartCodeViewerTheme` instances the encloses the given context. The default parameters is set within the `DartCodeViewer`. 110 | 111 | `DartCodeViewerThemeData` holds the color, size, and text styles for a dart code viewer theme. Use this class to configure a [DartCodeViewerThemeData] widget. To obtain the current ambient dart code viewer theme, use [DartCodeViewerTheme.of]. 112 | 113 | The simplest way to create a DartCodeThemeData is to use the [copyWith] on the one you get from [DartCodeViewerTheme.of], or create an entirely new one with [DartCodeViewerThemeData]. 114 | 115 | Here is an example below: 116 | 117 | ```dart 118 | DartCodeViewerTheme( 119 | data: DartCodeViewerThemeData( 120 | backgroundColor: Colors.pink, 121 | copyButtonText: Text('Copiar'), 122 | ), 123 | child: DartCodeViewer.textColor(r''' 124 | void main() { 125 | runApp(MyApp()); 126 | } 127 | ''', 128 | textStyle: GoogleFonts.lato(), 129 | commentColor: Colors.grey, 130 | baseColor: Colors.pink, 131 | ), 132 | ), 133 | ``` 134 | 135 | You can use this [Code viewer tool](https://romannurik.github.io/SlidesCodeHighlighter/) to help you with Theming. On the left side you put your example code and on the right you can choose the colors you want the code viewer to display. Below is a table that shows you the difference between the code viewer tool and the dart code viewer parameter. 136 | 137 | | Code Viewer Tool | Dart Code Viewer Parameter | 138 | | --- | --- | 139 | | background | backgroundColor | 140 | | plain text | baseStyle | 141 | | Punctuation | punctuationStyle | 142 | | String, values | stringStyle | 143 | | Keywords, tags | keywordStyle | 144 | | Comments | commentStyle | 145 | | Types | classStyle | 146 | | Numbers | numberStyle | 147 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | analyzer: 2 | enable-experiment: 3 | - non-nullable 4 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | **/ios/Flutter/.last_build_id 26 | .dart_tool/ 27 | .flutter-plugins 28 | .flutter-plugins-dependencies 29 | .packages 30 | .pub-cache/ 31 | .pub/ 32 | /build/ 33 | 34 | # Web related 35 | lib/generated_plugin_registrant.dart 36 | 37 | # Symbolication related 38 | app.*.symbols 39 | 40 | # Obfuscation related 41 | app.*.map.json 42 | 43 | # Exceptions to above rules. 44 | !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages 45 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 4d8553251a9b078a14c2623fe00b05eec44a6689 8 | channel: chips_text_scaling 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # dart_code_viewer_sample_app 2 | 3 | This example application demonstrates how to use [`dart_code_viewer`](https://pub.dev/packages/dart_code_viewer_sample_app) within a simple Flutter app. 4 | 5 | Try this example with Dartpad [here](dartpad.dev/embed-flutter.html?gh_owner=JoseAlba&gh_repo=dart-code-viewer&gh_path=example/lib) 6 | -------------------------------------------------------------------------------- /example/analysis_options.yaml: -------------------------------------------------------------------------------- 1 | analyzer: 2 | enable-experiment: 3 | - non-nullable 4 | -------------------------------------------------------------------------------- /example/android/.gitignore: -------------------------------------------------------------------------------- 1 | gradle-wrapper.jar 2 | /.gradle 3 | /captures/ 4 | /gradlew 5 | /gradlew.bat 6 | /local.properties 7 | GeneratedPluginRegistrant.java 8 | -------------------------------------------------------------------------------- /example/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | def localProperties = new Properties() 2 | def localPropertiesFile = rootProject.file('local.properties') 3 | if (localPropertiesFile.exists()) { 4 | localPropertiesFile.withReader('UTF-8') { reader -> 5 | localProperties.load(reader) 6 | } 7 | } 8 | 9 | def flutterRoot = localProperties.getProperty('flutter.sdk') 10 | if (flutterRoot == null) { 11 | throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") 12 | } 13 | 14 | def flutterVersionCode = localProperties.getProperty('flutter.versionCode') 15 | if (flutterVersionCode == null) { 16 | flutterVersionCode = '1' 17 | } 18 | 19 | def flutterVersionName = localProperties.getProperty('flutter.versionName') 20 | if (flutterVersionName == null) { 21 | flutterVersionName = '1.0' 22 | } 23 | 24 | apply plugin: 'com.android.application' 25 | apply plugin: 'kotlin-android' 26 | apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" 27 | 28 | android { 29 | compileSdkVersion 28 30 | 31 | sourceSets { 32 | main.java.srcDirs += 'src/main/kotlin' 33 | } 34 | 35 | lintOptions { 36 | disable 'InvalidPackage' 37 | } 38 | 39 | defaultConfig { 40 | // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). 41 | applicationId "com.example.example" 42 | minSdkVersion 16 43 | targetSdkVersion 28 44 | versionCode flutterVersionCode.toInteger() 45 | versionName flutterVersionName 46 | } 47 | 48 | buildTypes { 49 | release { 50 | // TODO: Add your own signing config for the release build. 51 | // Signing with the debug keys for now, so `flutter run --release` works. 52 | signingConfig signingConfigs.debug 53 | } 54 | } 55 | } 56 | 57 | flutter { 58 | source '../..' 59 | } 60 | 61 | dependencies { 62 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 63 | } 64 | -------------------------------------------------------------------------------- /example/android/app/src/debug/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 8 | 12 | 19 | 23 | 27 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 43 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /example/android/app/src/main/kotlin/com/example/example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.example 2 | 3 | import io.flutter.embedding.android.FlutterActivity 4 | 5 | class MainActivity: FlutterActivity() { 6 | } 7 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/drawable/launch_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/android/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/android/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /example/android/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 15 | 18 | 19 | -------------------------------------------------------------------------------- /example/android/app/src/profile/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext.kotlin_version = '1.3.50' 3 | repositories { 4 | google() 5 | jcenter() 6 | } 7 | 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:3.5.0' 10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 11 | } 12 | } 13 | 14 | allprojects { 15 | repositories { 16 | google() 17 | jcenter() 18 | } 19 | } 20 | 21 | rootProject.buildDir = '../build' 22 | subprojects { 23 | project.buildDir = "${rootProject.buildDir}/${project.name}" 24 | } 25 | subprojects { 26 | project.evaluationDependsOn(':app') 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /example/android/gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.jvmargs=-Xmx1536M 2 | android.enableR8=true 3 | android.useAndroidX=true 4 | android.enableJetifier=true 5 | -------------------------------------------------------------------------------- /example/android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jun 23 08:50:38 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip 7 | -------------------------------------------------------------------------------- /example/android/settings.gradle: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Flutter Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | include ':app' 6 | 7 | def localPropertiesFile = new File(rootProject.projectDir, "local.properties") 8 | def properties = new Properties() 9 | 10 | assert localPropertiesFile.exists() 11 | localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } 12 | 13 | def flutterSdkPath = properties.getProperty("flutter.sdk") 14 | assert flutterSdkPath != null, "flutter.sdk not set in local.properties" 15 | apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle" 16 | -------------------------------------------------------------------------------- /example/ios/.gitignore: -------------------------------------------------------------------------------- 1 | *.mode1v3 2 | *.mode2v3 3 | *.moved-aside 4 | *.pbxuser 5 | *.perspectivev3 6 | **/*sync/ 7 | .sconsign.dblite 8 | .tags* 9 | **/.vagrant/ 10 | **/DerivedData/ 11 | Icon? 12 | **/Pods/ 13 | **/.symlinks/ 14 | profile 15 | xcuserdata 16 | **/.generated/ 17 | Flutter/App.framework 18 | Flutter/Flutter.framework 19 | Flutter/Flutter.podspec 20 | Flutter/Generated.xcconfig 21 | Flutter/app.flx 22 | Flutter/app.zip 23 | Flutter/flutter_assets/ 24 | Flutter/flutter_export_environment.sh 25 | ServiceDefinitions.json 26 | Runner/GeneratedPluginRegistrant.* 27 | 28 | # Exceptions to above rules. 29 | !default.mode1v3 30 | !default.mode2v3 31 | !default.pbxuser 32 | !default.perspectivev3 33 | -------------------------------------------------------------------------------- /example/ios/Flutter/AppFrameworkInfo.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | App 9 | CFBundleIdentifier 10 | io.flutter.flutter.app 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | App 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1.0 23 | MinimumOSVersion 24 | 8.0 25 | 26 | 27 | -------------------------------------------------------------------------------- /example/ios/Flutter/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Flutter/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/ios/Podfile: -------------------------------------------------------------------------------- 1 | # Uncomment this line to define a global platform for your project 2 | # platform :ios, '9.0' 3 | 4 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 5 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 6 | 7 | project 'Runner', { 8 | 'Debug' => :debug, 9 | 'Profile' => :release, 10 | 'Release' => :release, 11 | } 12 | 13 | def flutter_root 14 | generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) 15 | unless File.exist?(generated_xcode_build_settings_path) 16 | raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" 17 | end 18 | 19 | File.foreach(generated_xcode_build_settings_path) do |line| 20 | matches = line.match(/FLUTTER_ROOT\=(.*)/) 21 | return matches[1].strip if matches 22 | end 23 | raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" 24 | end 25 | 26 | require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 27 | 28 | flutter_ios_podfile_setup 29 | 30 | target 'Runner' do 31 | use_frameworks! 32 | use_modular_headers! 33 | 34 | flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) 35 | end 36 | 37 | post_install do |installer| 38 | installer.pods_project.targets.each do |target| 39 | flutter_additional_ios_build_settings(target) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /example/ios/Podfile.lock: -------------------------------------------------------------------------------- 1 | PODS: 2 | - Flutter (1.0.0) 3 | - path_provider (0.0.1): 4 | - Flutter 5 | 6 | DEPENDENCIES: 7 | - Flutter (from `Flutter`) 8 | - path_provider (from `.symlinks/plugins/path_provider/ios`) 9 | 10 | EXTERNAL SOURCES: 11 | Flutter: 12 | :path: Flutter 13 | path_provider: 14 | :path: ".symlinks/plugins/path_provider/ios" 15 | 16 | SPEC CHECKSUMS: 17 | Flutter: 0e3d915762c693b495b44d77113d4970485de6ec 18 | path_provider: abfe2b5c733d04e238b0d8691db0cfd63a27a93c 19 | 20 | PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c 21 | 22 | COCOAPODS: 1.9.1 23 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PreviewsEnabled 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/ios/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import Flutter 3 | 4 | @UIApplicationMain 5 | @objc class AppDelegate: FlutterAppDelegate { 6 | override func application( 7 | _ application: UIApplication, 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? 9 | ) -> Bool { 10 | GeneratedPluginRegistrant.register(with: self) 11 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "20x20", 5 | "idiom" : "iphone", 6 | "filename" : "Icon-App-20x20@2x.png", 7 | "scale" : "2x" 8 | }, 9 | { 10 | "size" : "20x20", 11 | "idiom" : "iphone", 12 | "filename" : "Icon-App-20x20@3x.png", 13 | "scale" : "3x" 14 | }, 15 | { 16 | "size" : "29x29", 17 | "idiom" : "iphone", 18 | "filename" : "Icon-App-29x29@1x.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "29x29", 23 | "idiom" : "iphone", 24 | "filename" : "Icon-App-29x29@2x.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "29x29", 29 | "idiom" : "iphone", 30 | "filename" : "Icon-App-29x29@3x.png", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "size" : "40x40", 35 | "idiom" : "iphone", 36 | "filename" : "Icon-App-40x40@2x.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "40x40", 41 | "idiom" : "iphone", 42 | "filename" : "Icon-App-40x40@3x.png", 43 | "scale" : "3x" 44 | }, 45 | { 46 | "size" : "60x60", 47 | "idiom" : "iphone", 48 | "filename" : "Icon-App-60x60@2x.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "60x60", 53 | "idiom" : "iphone", 54 | "filename" : "Icon-App-60x60@3x.png", 55 | "scale" : "3x" 56 | }, 57 | { 58 | "size" : "20x20", 59 | "idiom" : "ipad", 60 | "filename" : "Icon-App-20x20@1x.png", 61 | "scale" : "1x" 62 | }, 63 | { 64 | "size" : "20x20", 65 | "idiom" : "ipad", 66 | "filename" : "Icon-App-20x20@2x.png", 67 | "scale" : "2x" 68 | }, 69 | { 70 | "size" : "29x29", 71 | "idiom" : "ipad", 72 | "filename" : "Icon-App-29x29@1x.png", 73 | "scale" : "1x" 74 | }, 75 | { 76 | "size" : "29x29", 77 | "idiom" : "ipad", 78 | "filename" : "Icon-App-29x29@2x.png", 79 | "scale" : "2x" 80 | }, 81 | { 82 | "size" : "40x40", 83 | "idiom" : "ipad", 84 | "filename" : "Icon-App-40x40@1x.png", 85 | "scale" : "1x" 86 | }, 87 | { 88 | "size" : "40x40", 89 | "idiom" : "ipad", 90 | "filename" : "Icon-App-40x40@2x.png", 91 | "scale" : "2x" 92 | }, 93 | { 94 | "size" : "76x76", 95 | "idiom" : "ipad", 96 | "filename" : "Icon-App-76x76@1x.png", 97 | "scale" : "1x" 98 | }, 99 | { 100 | "size" : "76x76", 101 | "idiom" : "ipad", 102 | "filename" : "Icon-App-76x76@2x.png", 103 | "scale" : "2x" 104 | }, 105 | { 106 | "size" : "83.5x83.5", 107 | "idiom" : "ipad", 108 | "filename" : "Icon-App-83.5x83.5@2x.png", 109 | "scale" : "2x" 110 | }, 111 | { 112 | "size" : "1024x1024", 113 | "idiom" : "ios-marketing", 114 | "filename" : "Icon-App-1024x1024@1x.png", 115 | "scale" : "1x" 116 | } 117 | ], 118 | "info" : { 119 | "version" : 1, 120 | "author" : "xcode" 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-1024x1024@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-20x20@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-29x29@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-40x40@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-60x60@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@1x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-76x76@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/AppIcon.appiconset/Icon-App-83.5x83.5@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "filename" : "LaunchImage.png", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "universal", 10 | "filename" : "LaunchImage@2x.png", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "universal", 15 | "filename" : "LaunchImage@3x.png", 16 | "scale" : "3x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@2x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/ios/Runner/Assets.xcassets/LaunchImage.imageset/LaunchImage@3x.png -------------------------------------------------------------------------------- /example/ios/Runner/Assets.xcassets/LaunchImage.imageset/README.md: -------------------------------------------------------------------------------- 1 | # Launch Screen Assets 2 | 3 | You can customize the launch screen with your own desired assets by replacing the image files in this directory. 4 | 5 | You can also do it by opening your Flutter project's Xcode project with `open ios/Runner.xcworkspace`, selecting `Runner/Assets.xcassets` in the Project Navigator and dropping in the desired images. -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /example/ios/Runner/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /example/ios/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | example 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | $(FLUTTER_BUILD_NAME) 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UISupportedInterfaceOrientations 30 | 31 | UIInterfaceOrientationPortrait 32 | UIInterfaceOrientationLandscapeLeft 33 | UIInterfaceOrientationLandscapeRight 34 | 35 | UISupportedInterfaceOrientations~ipad 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationPortraitUpsideDown 39 | UIInterfaceOrientationLandscapeLeft 40 | UIInterfaceOrientationLandscapeRight 41 | 42 | UIViewControllerBasedStatusBarAppearance 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /example/ios/Runner/Runner-Bridging-Header.h: -------------------------------------------------------------------------------- 1 | #import "GeneratedPluginRegistrant.h" 2 | -------------------------------------------------------------------------------- /example/lib/dartpad_metadata.yaml: -------------------------------------------------------------------------------- 1 | name: A dartpad code viewer example 2 | mode: flutter 3 | files: 4 | - name: main.dart -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:dart_code_viewer/dart_code_viewer.dart'; 3 | 4 | void main() { 5 | runApp(MyApp()); 6 | } 7 | 8 | class MyApp extends StatefulWidget { 9 | @override 10 | _MyAppState createState() => _MyAppState(); 11 | } 12 | 13 | class _MyAppState extends State { 14 | late ThemeMode _themeMode; 15 | late IconData iconToggle; 16 | 17 | initState() { 18 | super.initState(); 19 | _themeMode = ThemeMode.system; 20 | iconToggle = Icons.toggle_on; 21 | } 22 | 23 | toggleThemeMode() { 24 | if (_themeMode == ThemeMode.light) { 25 | _themeMode = ThemeMode.dark; 26 | iconToggle = Icons.toggle_off; 27 | } else if (_themeMode == ThemeMode.dark) { 28 | _themeMode = ThemeMode.light; 29 | iconToggle = Icons.toggle_on; 30 | } else { 31 | if (Theme.of(context).brightness == Brightness.light) { 32 | _themeMode = ThemeMode.dark; 33 | iconToggle = Icons.toggle_off; 34 | } else { 35 | _themeMode = ThemeMode.light; 36 | iconToggle = Icons.toggle_on; 37 | } 38 | } 39 | } 40 | 41 | @override 42 | Widget build(BuildContext context) { 43 | return MaterialApp( 44 | title: 'Flutter Demo', 45 | theme: ThemeData.light(), 46 | darkTheme: ThemeData.dark(), 47 | themeMode: _themeMode, 48 | home: Scaffold( 49 | appBar: AppBar( 50 | title: Text('Dart Code Viewer Example'), 51 | actions: [ 52 | IconButton( 53 | icon: Icon(iconToggle), 54 | onPressed: () { 55 | setState(() { 56 | toggleThemeMode(); 57 | }); 58 | }, 59 | ), 60 | ], 61 | ), 62 | body: DartCodeViewer(DartCode.template), 63 | ), 64 | ); 65 | } 66 | } 67 | 68 | class DartCode { 69 | static const template = ''' 70 | import 'package:flutter/material.dart'; 71 | import 'package:flutter/services.dart'; 72 | 73 | import 'dart_code_viewer_theme.dart'; 74 | import 'pre_highlighter.dart'; 75 | 76 | /// A code viewer for the dart language. 77 | /// 78 | /// A code viewer can be used to display dart code. By default the [DartCodeViewer] 79 | /// gives you a Theme based code view. If you are using a [ThemeMode] that is light 80 | /// than you will get the light option. Note that the default background of the code 81 | /// viewer is based off [ColorScheme.background]. 82 | /// 83 | /// Supplying a non-null [data] String is required as input. 84 | /// 85 | /// Requires one of its ancestors to be a [Material] widget. 86 | /// 87 | /// Requires one of its ancestors to be a [MediaQuery] widget. Typically, these 88 | /// are introduced by the [MaterialApp] or [WidgetsApp] widget at the top of 89 | /// your application widget tree. 90 | /// 91 | /// {@tool dartpad --template=stateless_widget_scaffold} 92 | /// 93 | /// ![A dart_code_viewer example for light mode.] 94 | /// (https://github.com/JoseAlba/dart_code_viewer/images/import_example) 95 | /// 96 | /// Here is an example of a small string that shows up as Dart code in a flutter 97 | /// application. 98 | /// 99 | /// ```dart 100 | /// @override 101 | /// Widget build(BuildContext context) { 102 | /// return DartCodeViewer(r'class DartCodeViewer extends StatelessWidget {}'); 103 | /// } 104 | /// ``` 105 | /// {@end-tool} 106 | /// 107 | /// See also: 108 | /// * [DartCodeViewerTheme] and [DartCodeViewerThemeData] for information about 109 | /// controlling the visual appearance of the DartCodeViewer. 110 | /// * [Code viewer online tool](https://romannurik.github.io/SlidesCodeHighlighter/) 111 | /// is a useful tool that lets you choose the color for each different style. 112 | /// On the left side you put your example code and on the right you can choose 113 | /// the colors you want the code viewer to display. 114 | /// background => backgroundColor 115 | /// plain text => baseStyle 116 | /// Punctuation => punctuationStyle 117 | /// String, values => stringStyle 118 | /// Keywords, tags => keywordStyle 119 | /// Comments => commentStyle 120 | /// Types => classStyle 121 | /// Numbers => numberStyle 122 | /// Declarations => constantStyle 123 | /// * [MediaQuery], from which the default height and width factor is obtained. 124 | class DartCodeViewer extends StatelessWidget { 125 | /// DartCodeViewer requires a [String] that will be the code shown within the 126 | /// code viewer. This should be dart code and it is preferable if you use a raw 127 | /// string by adding an r before the string. 128 | const DartCodeViewer( 129 | this.data, { 130 | Key? key, 131 | this.baseStyle, 132 | this.classStyle, 133 | this.commentStyle, 134 | this.constantStyle, 135 | this.keywordStyle, 136 | this.numberStyle, 137 | this.punctuationStyle, 138 | this.stringStyle, 139 | this.backgroundColor, 140 | this.copyButtonText, 141 | this.showCopyButton, 142 | this.height, 143 | this.width, 144 | }) : super(key: key); 145 | 146 | /// Create a DartCodeViewer based of one [TextStyle]. Optional [Color] parameters 147 | /// which change the TextStyle color for that highlighter type. 148 | /// 149 | /// The default [TextStyle] is [RobotoMono]. 150 | /// 151 | /// Useful parameter when you want to use one [TextStyle]. 152 | factory DartCodeViewer.textColor( 153 | String data, { 154 | TextStyle? textStyle, 155 | Color? baseColor, 156 | Color? classColor, 157 | Color? commentColor, 158 | Color? constantColor, 159 | Color? keywordColor, 160 | Color? numberColor, 161 | Color? punctuationColor, 162 | Color? stringColor, 163 | Color? backgroundColor, 164 | Text? copyButtonText, 165 | bool? showCopyButton, 166 | double? height, 167 | double? width, 168 | }) { 169 | return DartCodeViewer( 170 | data, 171 | baseStyle: textStyle?.copyWith(color: baseColor), 172 | classStyle: textStyle?.copyWith(color: classColor), 173 | commentStyle: textStyle?.copyWith(color: commentColor), 174 | constantStyle: textStyle?.copyWith(color: constantColor), 175 | keywordStyle: textStyle?.copyWith(color: keywordColor), 176 | numberStyle: textStyle?.copyWith(color: numberColor), 177 | punctuationStyle: textStyle?.copyWith(color: punctuationColor), 178 | stringStyle: textStyle?.copyWith(color: stringColor), 179 | backgroundColor: backgroundColor, 180 | copyButtonText: copyButtonText, 181 | showCopyButton: showCopyButton, 182 | height: height, 183 | width: width, 184 | ); 185 | } 186 | 187 | /// Common code viewer highlighter for [ThemeMode.light]. 188 | factory DartCodeViewer.light(String data) { 189 | return DartCodeViewer.textColor( 190 | data, 191 | baseColor: Colors.blueGrey.shade800, 192 | classColor: Colors.purple.shade500, 193 | commentColor: Colors.pink.shade600, 194 | constantColor: Colors.indigo.shade500, 195 | keywordColor: Colors.indigo.shade500, 196 | numberColor: Colors.red.shade700, 197 | punctuationColor: Colors.blueGrey.shade800, 198 | stringColor: Colors.green.shade700, 199 | backgroundColor: Colors.grey.shade100, 200 | ); 201 | } 202 | 203 | /// Code viewer light alternative for [ThemeMode.light]. 204 | factory DartCodeViewer.lightAlt(String data) { 205 | return DartCodeViewer.textColor( 206 | data, 207 | baseColor: Colors.black, 208 | classColor: Color(0xFF673AB7), 209 | commentColor: Color(0xFF999999), 210 | constantColor: Color(0xFFE67C73), 211 | keywordColor: Color(0xFF4285F4), 212 | numberColor: Color(0xFFDB4437), 213 | punctuationColor: Color(0xFFA3A3A3), 214 | stringColor: Color(0xFF0F9D58), 215 | backgroundColor: Color(0xFFEEEEEE), 216 | ); 217 | } 218 | 219 | /// Common code viewer highlighter for [ThemeMode.dark]. 220 | factory DartCodeViewer.dark(String data) { 221 | return DartCodeViewer.textColor( 222 | data, 223 | baseColor: Colors.blueGrey.shade50, 224 | classColor: Colors.purple.shade200, 225 | commentColor: Colors.pink.shade300, 226 | constantColor: Colors.yellow.shade700, 227 | keywordColor: Colors.cyan.shade300, 228 | numberColor: Colors.yellow.shade700, 229 | punctuationColor: Colors.blueGrey.shade50, 230 | stringColor: Colors.lightGreen.shade400, 231 | backgroundColor: Colors.grey.shade900, 232 | ); 233 | } 234 | 235 | /// Code viewer dark alternative for [ThemeMode.dark]. 236 | factory DartCodeViewer.darkAlt(String data) { 237 | return DartCodeViewer.textColor( 238 | data, 239 | baseColor: Colors.white, 240 | classColor: Color(0xFFFF8A65), 241 | commentColor: Color(0xFFAAAAAA), 242 | constantColor: Color(0xFFE67C73), 243 | keywordColor: Color(0xFF7BAAF7), 244 | numberColor: Color(0xFFF4B400), 245 | punctuationColor: Color(0xFFA3A3A3), 246 | stringColor: Color(0xFF57BB8A), 247 | backgroundColor: Color(0xFF000000), 248 | ); 249 | } 250 | 251 | /// Code viewer highlighter with a great dark design for [ThemeMode.dark]. 252 | factory DartCodeViewer.designDark(String data) { 253 | return DartCodeViewer.textColor( 254 | data, 255 | baseColor: Colors.white, 256 | classColor: Color(0xFFFF8A80), 257 | commentColor: Color(0xFF607D8B), 258 | constantColor: Color(0xFF90A4AE), 259 | keywordColor: Color(0xFF26C6DA), 260 | numberColor: Color(0xFFFFBC00), 261 | punctuationColor: Color(0xFF90A4AE), 262 | stringColor: Color(0xFF00BFA4), 263 | backgroundColor: Color(0xFF263238), 264 | ); 265 | } 266 | 267 | /// Code viewer highlighter for Google IO 2017. 268 | factory DartCodeViewer.io17(String data) { 269 | return DartCodeViewer.textColor( 270 | data, 271 | baseColor: Colors.white, 272 | classColor: Color(0xFFFF8857), 273 | commentColor: Color(0xFFFF5CB4), 274 | constantColor: Color(0xFF90A4AE), 275 | keywordColor: Color(0xFF00E4FF), 276 | numberColor: Color(0xFFFFD500), 277 | punctuationColor: Color(0xFF90A4AE), 278 | stringColor: Color(0xFF1CE8b5), 279 | backgroundColor: Color(0xFF263238), 280 | ); 281 | } 282 | 283 | /// Code viewer highlighter for Google IO 2019. 284 | factory DartCodeViewer.io19(String data) { 285 | return DartCodeViewer.textColor( 286 | data, 287 | baseColor: Colors.white, 288 | classColor: Color(0xFFEE675C), 289 | commentColor: Color(0xFF9AA0A6), 290 | constantColor: Color(0xFFFCC934), 291 | keywordColor: Color(0xFF669DF6), 292 | numberColor: Color(0xFFFCC934), 293 | punctuationColor: Color(0xFF9AA0A6), 294 | stringColor: Color(0xFF5BB974), 295 | backgroundColor: Color(0xFF202124), 296 | ); 297 | } 298 | 299 | /// Code viewer highlighter for Flutter Interact 2019. 300 | factory DartCodeViewer.flutterInteract19(String data) { 301 | return DartCodeViewer.textColor( 302 | data, 303 | baseColor: Color(0xFFFAFBFB), 304 | classColor: Color(0xFFD65BAD), 305 | commentColor: Color(0xFF808080), 306 | constantColor: Color(0xFFFF8383), 307 | keywordColor: Color(0xFF1CDEC9), 308 | numberColor: Color(0xFFBD93F9), 309 | punctuationColor: Color(0xFF8BE9FD), 310 | stringColor: Color(0xFFffa65c), 311 | backgroundColor: Color(0xFF241e30), 312 | ); 313 | } 314 | 315 | /// The string that is transformed into code. This is a required variable. 316 | final String data; 317 | 318 | /// The text style for the plain text in code. 319 | final TextStyle? baseStyle; 320 | 321 | /// The text style for the code types in the code. 322 | /// 323 | /// For example: 324 | /// * The class name. 325 | /// * StatelessWidget and StatefulWidget. 326 | final TextStyle? classStyle; 327 | 328 | /// The text style for the commented out code. 329 | final TextStyle? commentStyle; 330 | 331 | /// The text style for the constant style code. 332 | final TextStyle? constantStyle; 333 | 334 | /// The text style for keywords. For example: 335 | /// * else 336 | /// * enum 337 | /// * export 338 | /// * external 339 | /// * factory 340 | /// * false 341 | final TextStyle? keywordStyle; 342 | 343 | /// The text style for numbers within the code. 344 | final TextStyle? numberStyle; 345 | 346 | /// The text style for punctuation code like periods and commas. 347 | final TextStyle? punctuationStyle; 348 | 349 | /// The text style for Strings. For example the data when using the [Text] widget. 350 | final TextStyle? stringStyle; 351 | 352 | /// The background Color of the code. By default it is [Theme.of(context).colorScheme.background]. 353 | final Color? backgroundColor; 354 | 355 | /// The text shown in the copy button by default it is 'COPY ALL'. 356 | final Text? copyButtonText; 357 | 358 | /// Shows copy button that lets user copy all the code as a raw string. By 359 | /// default the button is showing. 360 | final bool? showCopyButton; 361 | 362 | /// The height of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.height] 363 | final double? height; 364 | 365 | /// The width of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.width] 366 | final double? width; 367 | 368 | @override 369 | Widget build(BuildContext context) { 370 | final codeTextStyle = Theme.of(context).textTheme.bodyText1; 371 | 372 | final lightModeOn = Theme.of(context).brightness == Brightness.light; 373 | 374 | // These are defaults for the different types of text styles. The default 375 | // returns two different types of styles depending on the brightness of the 376 | // application. 377 | final _defaultBaseStyle = codeTextStyle?.copyWith( 378 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50, 379 | ); 380 | final _defaultClassStyle = codeTextStyle?.copyWith( 381 | color: lightModeOn ? Colors.purple.shade500 : Colors.purple.shade200, 382 | ); 383 | final _defaultCommentStyle = codeTextStyle?.copyWith( 384 | color: lightModeOn ? Colors.pink.shade600 : Colors.pink.shade300, 385 | ); 386 | final _defaultConstantStyle = codeTextStyle?.copyWith( 387 | color: lightModeOn ? Colors.indigo.shade500 : Colors.yellow.shade700, 388 | ); 389 | final _defaultKeywordStyle = codeTextStyle?.copyWith( 390 | color: lightModeOn ? Colors.indigo.shade500 : Colors.cyan.shade300, 391 | ); 392 | final _defaultNumberStyle = codeTextStyle?.copyWith( 393 | color: lightModeOn ? Colors.red.shade700 : Colors.yellow.shade700, 394 | ); 395 | final _defaultPunctuationalStyle = codeTextStyle?.copyWith( 396 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50, 397 | ); 398 | final _defaultStringStyle = codeTextStyle?.copyWith( 399 | color: lightModeOn ? Colors.green.shade700 : Colors.lightGreen.shade400, 400 | ); 401 | 402 | final _defaultCopyButtonText = Text('COPY ALL'); 403 | final _defaultShowCopyButton = true; 404 | 405 | var dartCodeViewerThemeData = DartCodeViewerTheme.of(context); 406 | dartCodeViewerThemeData = dartCodeViewerThemeData.copyWith( 407 | baseStyle: 408 | baseStyle ?? dartCodeViewerThemeData.baseStyle ?? _defaultBaseStyle, 409 | classStyle: classStyle ?? 410 | dartCodeViewerThemeData.classStyle ?? 411 | _defaultClassStyle, 412 | commentStyle: commentStyle ?? 413 | dartCodeViewerThemeData.commentStyle ?? 414 | _defaultCommentStyle, 415 | constantStyle: constantStyle ?? 416 | dartCodeViewerThemeData.constantStyle ?? 417 | _defaultConstantStyle, 418 | keywordStyle: keywordStyle ?? 419 | dartCodeViewerThemeData.keywordStyle ?? 420 | _defaultKeywordStyle, 421 | numberStyle: numberStyle ?? 422 | dartCodeViewerThemeData.numberStyle ?? 423 | _defaultNumberStyle, 424 | punctuationStyle: punctuationStyle ?? 425 | dartCodeViewerThemeData.punctuationStyle ?? 426 | _defaultPunctuationalStyle, 427 | stringStyle: stringStyle ?? 428 | dartCodeViewerThemeData.stringStyle ?? 429 | _defaultStringStyle, 430 | backgroundColor: backgroundColor ?? 431 | dartCodeViewerThemeData.backgroundColor ?? 432 | Theme.of(context).colorScheme.background, 433 | copyButtonText: copyButtonText ?? 434 | dartCodeViewerThemeData.copyButtonText ?? 435 | _defaultCopyButtonText, 436 | showCopyButton: showCopyButton ?? 437 | dartCodeViewerThemeData.showCopyButton ?? 438 | _defaultShowCopyButton, 439 | height: height ?? 440 | dartCodeViewerThemeData.height ?? 441 | MediaQuery.of(context).size.height, 442 | width: width ?? 443 | dartCodeViewerThemeData.width ?? 444 | MediaQuery.of(context).size.width, 445 | ); 446 | 447 | return DartCodeViewerTheme( 448 | data: dartCodeViewerThemeData, 449 | child: Container( 450 | color: dartCodeViewerThemeData.backgroundColor, 451 | padding: const EdgeInsets.symmetric(horizontal: 16), 452 | height: dartCodeViewerThemeData.height, 453 | width: dartCodeViewerThemeData.width, 454 | child: _DartCodeViewerPage( 455 | codifyString(data, dartCodeViewerThemeData), 456 | ), 457 | ), 458 | ); 459 | } 460 | 461 | InlineSpan codifyString( 462 | String content, 463 | DartCodeViewerThemeData dartCodeViewerThemeData, 464 | ) { 465 | var textSpans = []; 466 | final codeSpans = DartSyntaxPreHighlighter().format(content); 467 | // Converting CodeSpan to TextSpan by first converting to a string and then TextSpan. 468 | for (final span in codeSpans) { 469 | textSpans.add(stringToTextSpan(span.toString(), dartCodeViewerThemeData)); 470 | } 471 | return TextSpan(children: textSpans); 472 | } 473 | 474 | TextSpan stringToTextSpan( 475 | String string, 476 | DartCodeViewerThemeData dartCodeViewerThemeData, 477 | ) { 478 | return TextSpan( 479 | style: () { 480 | final String? styleString = 481 | RegExp(r'codeStyle.\w*').firstMatch(string)?.group(0); 482 | var dartCodeViewerTheme = dartCodeViewerThemeData; 483 | 484 | switch (styleString) { 485 | case 'codeStyle.baseStyle': 486 | return dartCodeViewerTheme.baseStyle; 487 | case 'codeStyle.numberStyle': 488 | return dartCodeViewerTheme.numberStyle; 489 | case 'codeStyle.commentStyle': 490 | return dartCodeViewerTheme.commentStyle; 491 | case 'codeStyle.keywordStyle': 492 | return dartCodeViewerTheme.keywordStyle; 493 | case 'codeStyle.stringStyle': 494 | return dartCodeViewerTheme.stringStyle; 495 | case 'codeStyle.punctuationStyle': 496 | return dartCodeViewerTheme.punctuationStyle; 497 | case 'codeStyle.classStyle': 498 | return dartCodeViewerTheme.classStyle; 499 | case 'codeStyle.constantStyle': 500 | return dartCodeViewerTheme.constantStyle; 501 | default: 502 | return dartCodeViewerTheme.baseStyle; 503 | } 504 | }(), 505 | text: () { 506 | final textString = RegExp('\'.*\'').firstMatch(string)?.group(0); 507 | final subString = textString!.substring(1, textString.length - 1); 508 | return decodeString(subString); 509 | }(), 510 | ); 511 | } 512 | 513 | /// Read raw string as regular String. Converts Unicode characters to actual 514 | /// numbers. 515 | String decodeString(String string) { 516 | return string 517 | .replaceAll(r'\u000a', '\n') 518 | .replaceAll(r'\u0027', '\'') 519 | .replaceAll(r'\u0009', '\t'); 520 | } 521 | } 522 | 523 | class _DartCodeViewerPage extends StatelessWidget { 524 | const _DartCodeViewerPage(this.code); 525 | final InlineSpan code; 526 | 527 | @override 528 | Widget build(BuildContext context) { 529 | final _richTextCode = code; 530 | final _plainTextCode = _richTextCode.toPlainText(); 531 | 532 | void _showSnackBarOnCopySuccess(dynamic result) { 533 | ScaffoldMessenger.of(context).showSnackBar( 534 | SnackBar( 535 | content: Text('Copied to Clipboard'), 536 | ), 537 | ); 538 | } 539 | 540 | void _showSnackBarOnCopyFailure(Object exception) { 541 | ScaffoldMessenger.of(context).showSnackBar( 542 | SnackBar( 543 | content: Text('Failure to copy to clipboard: \$exception'), 544 | ), 545 | ); 546 | } 547 | 548 | return Column( 549 | crossAxisAlignment: CrossAxisAlignment.start, 550 | children: [ 551 | if (DartCodeViewerTheme.of(context).showCopyButton!) 552 | ElevatedButton( 553 | onPressed: () async { 554 | await Clipboard.setData(ClipboardData(text: _plainTextCode)) 555 | .then(_showSnackBarOnCopySuccess) 556 | .catchError(_showSnackBarOnCopyFailure); 557 | }, 558 | child: DartCodeViewerTheme.of(context).copyButtonText, 559 | ), 560 | Expanded( 561 | child: SingleChildScrollView( 562 | child: RichText( 563 | textDirection: TextDirection.ltr, 564 | text: _richTextCode, 565 | ), 566 | ), 567 | ), 568 | ], 569 | ); 570 | } 571 | } 572 | '''; 573 | } 574 | -------------------------------------------------------------------------------- /example/macos/.gitignore: -------------------------------------------------------------------------------- 1 | # Flutter-related 2 | **/Flutter/ephemeral/ 3 | **/Pods/ 4 | 5 | # Xcode-related 6 | **/xcuserdata/ 7 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/Flutter-Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" 2 | #include "ephemeral/Flutter-Generated.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Flutter/GeneratedPluginRegistrant.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | import FlutterMacOS 6 | import Foundation 7 | 8 | 9 | func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { 10 | } 11 | -------------------------------------------------------------------------------- /example/macos/Podfile: -------------------------------------------------------------------------------- 1 | platform :osx, '10.11' 2 | 3 | # CocoaPods analytics sends network stats synchronously affecting flutter build latency. 4 | ENV['COCOAPODS_DISABLE_STATS'] = 'true' 5 | 6 | project 'Runner', { 7 | 'Debug' => :debug, 8 | 'Profile' => :release, 9 | 'Release' => :release, 10 | } 11 | 12 | def parse_KV_file(file, separator='=') 13 | file_abs_path = File.expand_path(file) 14 | if !File.exists? file_abs_path 15 | return []; 16 | end 17 | pods_ary = [] 18 | skip_line_start_symbols = ["#", "/"] 19 | File.foreach(file_abs_path) { |line| 20 | next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ } 21 | plugin = line.split(pattern=separator) 22 | if plugin.length == 2 23 | podname = plugin[0].strip() 24 | path = plugin[1].strip() 25 | podpath = File.expand_path("#{path}", file_abs_path) 26 | pods_ary.push({:name => podname, :path => podpath}); 27 | else 28 | puts "Invalid plugin specification: #{line}" 29 | end 30 | } 31 | return pods_ary 32 | end 33 | 34 | def pubspec_supports_macos(file) 35 | file_abs_path = File.expand_path(file) 36 | if !File.exists? file_abs_path 37 | return false; 38 | end 39 | File.foreach(file_abs_path) { |line| 40 | return true if line =~ /^\s*macos:/ 41 | } 42 | return false 43 | end 44 | 45 | target 'Runner' do 46 | use_frameworks! 47 | use_modular_headers! 48 | 49 | # Prepare symlinks folder. We use symlinks to avoid having Podfile.lock 50 | # referring to absolute paths on developers' machines. 51 | ephemeral_dir = File.join('Flutter', 'ephemeral') 52 | symlink_dir = File.join(ephemeral_dir, '.symlinks') 53 | symlink_plugins_dir = File.join(symlink_dir, 'plugins') 54 | system("rm -rf #{symlink_dir}") 55 | system("mkdir -p #{symlink_plugins_dir}") 56 | 57 | # Flutter Pods 58 | generated_xcconfig = parse_KV_file(File.join(ephemeral_dir, 'Flutter-Generated.xcconfig')) 59 | if generated_xcconfig.empty? 60 | puts "Flutter-Generated.xcconfig must exist. If you're running pod install manually, make sure flutter packages get is executed first." 61 | end 62 | generated_xcconfig.map { |p| 63 | if p[:name] == 'FLUTTER_FRAMEWORK_DIR' 64 | symlink = File.join(symlink_dir, 'flutter') 65 | File.symlink(File.dirname(p[:path]), symlink) 66 | pod 'FlutterMacOS', :path => File.join(symlink, File.basename(p[:path])) 67 | end 68 | } 69 | 70 | # Plugin Pods 71 | plugin_pods = parse_KV_file('../.flutter-plugins') 72 | plugin_pods.map { |p| 73 | symlink = File.join(symlink_plugins_dir, p[:name]) 74 | File.symlink(p[:path], symlink) 75 | if pubspec_supports_macos(File.join(symlink, 'pubspec.yaml')) 76 | pod p[:name], :path => File.join(symlink, 'macos') 77 | end 78 | } 79 | end 80 | 81 | # Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system. 82 | install! 'cocoapods', :disable_input_output_paths => true 83 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | 83 | 85 | 91 | 92 | 93 | 94 | 96 | 97 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/macos/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/macos/Runner/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | @NSApplicationMain 5 | class AppDelegate: FlutterAppDelegate { 6 | override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { 7 | return true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "size" : "16x16", 5 | "idiom" : "mac", 6 | "filename" : "app_icon_16.png", 7 | "scale" : "1x" 8 | }, 9 | { 10 | "size" : "16x16", 11 | "idiom" : "mac", 12 | "filename" : "app_icon_32.png", 13 | "scale" : "2x" 14 | }, 15 | { 16 | "size" : "32x32", 17 | "idiom" : "mac", 18 | "filename" : "app_icon_32.png", 19 | "scale" : "1x" 20 | }, 21 | { 22 | "size" : "32x32", 23 | "idiom" : "mac", 24 | "filename" : "app_icon_64.png", 25 | "scale" : "2x" 26 | }, 27 | { 28 | "size" : "128x128", 29 | "idiom" : "mac", 30 | "filename" : "app_icon_128.png", 31 | "scale" : "1x" 32 | }, 33 | { 34 | "size" : "128x128", 35 | "idiom" : "mac", 36 | "filename" : "app_icon_256.png", 37 | "scale" : "2x" 38 | }, 39 | { 40 | "size" : "256x256", 41 | "idiom" : "mac", 42 | "filename" : "app_icon_256.png", 43 | "scale" : "1x" 44 | }, 45 | { 46 | "size" : "256x256", 47 | "idiom" : "mac", 48 | "filename" : "app_icon_512.png", 49 | "scale" : "2x" 50 | }, 51 | { 52 | "size" : "512x512", 53 | "idiom" : "mac", 54 | "filename" : "app_icon_512.png", 55 | "scale" : "1x" 56 | }, 57 | { 58 | "size" : "512x512", 59 | "idiom" : "mac", 60 | "filename" : "app_icon_1024.png", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_1024.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_128.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_16.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_256.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_32.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_512.png -------------------------------------------------------------------------------- /example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/macos/Runner/Assets.xcassets/AppIcon.appiconset/app_icon_64.png -------------------------------------------------------------------------------- /example/macos/Runner/Base.lproj/MainMenu.xib: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/AppInfo.xcconfig: -------------------------------------------------------------------------------- 1 | // Application-level settings for the Runner target. 2 | // 3 | // This may be replaced with something auto-generated from metadata (e.g., pubspec.yaml) in the 4 | // future. If not, the values below would default to using the project name when this becomes a 5 | // 'flutter create' template. 6 | 7 | // The application's name. By default this is also the title of the Flutter window. 8 | PRODUCT_NAME = example 9 | 10 | // The application's bundle identifier 11 | PRODUCT_BUNDLE_IDENTIFIER = com.example.example 12 | 13 | // The copyright displayed in application information 14 | PRODUCT_COPYRIGHT = Copyright © 2020 com.example. All rights reserved. 15 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Debug.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Debug.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Release.xcconfig: -------------------------------------------------------------------------------- 1 | #include "../../Flutter/Flutter-Release.xcconfig" 2 | #include "Warnings.xcconfig" 3 | -------------------------------------------------------------------------------- /example/macos/Runner/Configs/Warnings.xcconfig: -------------------------------------------------------------------------------- 1 | WARNING_CFLAGS = -Wall -Wconditional-uninitialized -Wnullable-to-nonnull-conversion -Wmissing-method-return-type -Woverlength-strings 2 | GCC_WARN_UNDECLARED_SELECTOR = YES 3 | CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY = YES 4 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE 5 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES 6 | CLANG_WARN_PRAGMA_PACK = YES 7 | CLANG_WARN_STRICT_PROTOTYPES = YES 8 | CLANG_WARN_COMMA = YES 9 | GCC_WARN_STRICT_SELECTOR_MATCH = YES 10 | CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK = YES 11 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES 12 | GCC_WARN_SHADOW = YES 13 | CLANG_WARN_UNREACHABLE_CODE = YES 14 | -------------------------------------------------------------------------------- /example/macos/Runner/DebugProfile.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.cs.allow-jit 8 | 9 | com.apple.security.network.server 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /example/macos/Runner/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | $(FLUTTER_BUILD_NAME) 21 | CFBundleVersion 22 | $(FLUTTER_BUILD_NUMBER) 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | $(PRODUCT_COPYRIGHT) 27 | NSMainNibFile 28 | MainMenu 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | 33 | -------------------------------------------------------------------------------- /example/macos/Runner/MainFlutterWindow.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import FlutterMacOS 3 | 4 | class MainFlutterWindow: NSWindow { 5 | override func awakeFromNib() { 6 | let flutterViewController = FlutterViewController.init() 7 | let windowFrame = self.frame 8 | self.contentViewController = flutterViewController 9 | self.setFrame(windowFrame, display: true) 10 | 11 | RegisterGeneratedPlugins(registry: flutterViewController) 12 | 13 | super.awakeFromNib() 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/macos/Runner/Release.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dart_code_viewer_example 2 | description: A Flutter application showcasing how to use the dart_code_viewer package. 3 | 4 | # The following line prevents the package from being accidentally published to 5 | # pub.dev using `pub publish`. This is preferred for private packages. 6 | publish_to: 'none' # Remove this line if you wish to publish to pub.dev 7 | 8 | version: 0.0.1 9 | 10 | environment: 11 | sdk: ">=2.12.0-0 <3.0.0" 12 | 13 | dependencies: 14 | flutter: 15 | sdk: flutter 16 | 17 | cupertino_icons: ^0.1.3 18 | dart_code_viewer: 19 | path: ../ 20 | 21 | dev_dependencies: 22 | flutter_test: 23 | sdk: flutter 24 | 25 | flutter: 26 | uses-material-design: true 27 | -------------------------------------------------------------------------------- /example/web/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/web/favicon.png -------------------------------------------------------------------------------- /example/web/icons/Icon-192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/web/icons/Icon-192.png -------------------------------------------------------------------------------- /example/web/icons/Icon-512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/example/web/icons/Icon-512.png -------------------------------------------------------------------------------- /example/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | example 18 | 19 | 20 | 21 | 24 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /example/web/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "short_name": "example", 4 | "start_url": ".", 5 | "display": "standalone", 6 | "background_color": "#0175C2", 7 | "theme_color": "#0175C2", 8 | "description": "A new Flutter project.", 9 | "orientation": "portrait-primary", 10 | "prefer_related_applications": false, 11 | "icons": [ 12 | { 13 | "src": "icons/Icon-192.png", 14 | "sizes": "192x192", 15 | "type": "image/png" 16 | }, 17 | { 18 | "src": "icons/Icon-512.png", 19 | "sizes": "512x512", 20 | "type": "image/png" 21 | } 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /images/dart_code_viewer.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/images/dart_code_viewer.gif -------------------------------------------------------------------------------- /images/import_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JoseAlba/dart-code-viewer/b3c6372ce6cec712fe8372ce973d1dfbc99ab2fd/images/import_example.png -------------------------------------------------------------------------------- /lib/dart_code_viewer.dart: -------------------------------------------------------------------------------- 1 | library dart_code_viewer; 2 | 3 | export 'src/dart_code_viewer.dart'; 4 | export 'src/dart_code_viewer_theme.dart'; 5 | -------------------------------------------------------------------------------- /lib/src/dart_code_viewer.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter/services.dart'; 3 | 4 | import 'dart_code_viewer_theme.dart'; 5 | import 'pre_highlighter.dart'; 6 | 7 | /// A code viewer for the dart language. 8 | /// 9 | /// A code viewer can be used to display dart code. By default the [DartCodeViewer] 10 | /// gives you a Theme based code view. If you are using a [ThemeMode] that is light 11 | /// than you will get the light option. Note that the default background of the code 12 | /// viewer is based off [ColorScheme.background]. 13 | /// 14 | /// Supplying a non-null [data] String is required as input. 15 | /// 16 | /// Requires one of its ancestors to be a [Material] widget. 17 | /// 18 | /// Requires one of its ancestors to be a [MediaQuery] widget. Typically, these 19 | /// are introduced by the [MaterialApp] or [WidgetsApp] widget at the top of 20 | /// your application widget tree. 21 | /// 22 | /// {@tool dartpad --template=stateless_widget_scaffold} 23 | /// 24 | /// ![A dart_code_viewer example for light mode.] 25 | /// (https://github.com/JoseAlba/dart_code_viewer/images/import_example) 26 | /// 27 | /// Here is an example of a small string that shows up as Dart code in a flutter 28 | /// application. 29 | /// 30 | /// ```dart 31 | /// @override 32 | /// Widget build(BuildContext context) { 33 | /// return DartCodeViewer(r'class DartCodeViewer extends StatelessWidget {}'); 34 | /// } 35 | /// ``` 36 | /// {@end-tool} 37 | /// 38 | /// See also: 39 | /// * [DartCodeViewerTheme] and [DartCodeViewerThemeData] for information about 40 | /// controlling the visual appearance of the DartCodeViewer. 41 | /// * [Code viewer online tool](https://romannurik.github.io/SlidesCodeHighlighter/) 42 | /// is a useful tool that lets you choose the color for each different style. 43 | /// On the left side you put your example code and on the right you can choose 44 | /// the colors you want the code viewer to display. 45 | /// background => backgroundColor 46 | /// plain text => baseStyle 47 | /// Punctuation => punctuationStyle 48 | /// String, values => stringStyle 49 | /// Keywords, tags => keywordStyle 50 | /// Comments => commentStyle 51 | /// Types => classStyle 52 | /// Numbers => numberStyle 53 | /// Declarations => constantStyle 54 | /// * [MediaQuery], from which the default height and width factor is obtained. 55 | class DartCodeViewer extends StatelessWidget { 56 | /// DartCodeViewer requires a [String] that will be the code shown within the 57 | /// code viewer. This should be dart code and it is preferable if you use a raw 58 | /// string by adding an r before the string. 59 | const DartCodeViewer( 60 | this.data, { 61 | Key? key, 62 | this.baseStyle, 63 | this.classStyle, 64 | this.commentStyle, 65 | this.constantStyle, 66 | this.keywordStyle, 67 | this.numberStyle, 68 | this.punctuationStyle, 69 | this.stringStyle, 70 | this.backgroundColor, 71 | this.copyButtonText, 72 | this.showCopyButton, 73 | this.height, 74 | this.width, 75 | }) : super(key: key); 76 | 77 | /// Create a DartCodeViewer based of one [TextStyle]. Optional [Color] parameters 78 | /// which change the TextStyle color for that highlighter type. 79 | /// 80 | /// The default [TextStyle] is [RobotoMono]. 81 | /// 82 | /// Useful parameter when you want to use one [TextStyle]. 83 | factory DartCodeViewer.textColor( 84 | String data, { 85 | TextStyle? textStyle, 86 | Color? baseColor, 87 | Color? classColor, 88 | Color? commentColor, 89 | Color? constantColor, 90 | Color? keywordColor, 91 | Color? numberColor, 92 | Color? punctuationColor, 93 | Color? stringColor, 94 | Color? backgroundColor, 95 | Text? copyButtonText, 96 | bool? showCopyButton, 97 | double? height, 98 | double? width, 99 | }) { 100 | return DartCodeViewer( 101 | data, 102 | baseStyle: textStyle?.copyWith(color: baseColor), 103 | classStyle: textStyle?.copyWith(color: classColor), 104 | commentStyle: textStyle?.copyWith(color: commentColor), 105 | constantStyle: textStyle?.copyWith(color: constantColor), 106 | keywordStyle: textStyle?.copyWith(color: keywordColor), 107 | numberStyle: textStyle?.copyWith(color: numberColor), 108 | punctuationStyle: textStyle?.copyWith(color: punctuationColor), 109 | stringStyle: textStyle?.copyWith(color: stringColor), 110 | backgroundColor: backgroundColor, 111 | copyButtonText: copyButtonText, 112 | showCopyButton: showCopyButton, 113 | height: height, 114 | width: width, 115 | ); 116 | } 117 | 118 | /// Common code viewer highlighter for [ThemeMode.light]. 119 | factory DartCodeViewer.light(String data) { 120 | return DartCodeViewer.textColor( 121 | data, 122 | baseColor: Colors.blueGrey.shade800, 123 | classColor: Colors.purple.shade500, 124 | commentColor: Colors.pink.shade600, 125 | constantColor: Colors.indigo.shade500, 126 | keywordColor: Colors.indigo.shade500, 127 | numberColor: Colors.red.shade700, 128 | punctuationColor: Colors.blueGrey.shade800, 129 | stringColor: Colors.green.shade700, 130 | backgroundColor: Colors.grey.shade100, 131 | ); 132 | } 133 | 134 | /// Code viewer light alternative for [ThemeMode.light]. 135 | factory DartCodeViewer.lightAlt(String data) { 136 | return DartCodeViewer.textColor( 137 | data, 138 | baseColor: Colors.black, 139 | classColor: Color(0xFF673AB7), 140 | commentColor: Color(0xFF999999), 141 | constantColor: Color(0xFFE67C73), 142 | keywordColor: Color(0xFF4285F4), 143 | numberColor: Color(0xFFDB4437), 144 | punctuationColor: Color(0xFFA3A3A3), 145 | stringColor: Color(0xFF0F9D58), 146 | backgroundColor: Color(0xFFEEEEEE), 147 | ); 148 | } 149 | 150 | /// Common code viewer highlighter for [ThemeMode.dark]. 151 | factory DartCodeViewer.dark(String data) { 152 | return DartCodeViewer.textColor( 153 | data, 154 | baseColor: Colors.blueGrey.shade50, 155 | classColor: Colors.purple.shade200, 156 | commentColor: Colors.pink.shade300, 157 | constantColor: Colors.yellow.shade700, 158 | keywordColor: Colors.cyan.shade300, 159 | numberColor: Colors.yellow.shade700, 160 | punctuationColor: Colors.blueGrey.shade50, 161 | stringColor: Colors.lightGreen.shade400, 162 | backgroundColor: Colors.grey.shade900, 163 | ); 164 | } 165 | 166 | /// Code viewer dark alternative for [ThemeMode.dark]. 167 | factory DartCodeViewer.darkAlt(String data) { 168 | return DartCodeViewer.textColor( 169 | data, 170 | baseColor: Colors.white, 171 | classColor: Color(0xFFFF8A65), 172 | commentColor: Color(0xFFAAAAAA), 173 | constantColor: Color(0xFFE67C73), 174 | keywordColor: Color(0xFF7BAAF7), 175 | numberColor: Color(0xFFF4B400), 176 | punctuationColor: Color(0xFFA3A3A3), 177 | stringColor: Color(0xFF57BB8A), 178 | backgroundColor: Color(0xFF000000), 179 | ); 180 | } 181 | 182 | /// Code viewer highlighter with a great dark design for [ThemeMode.dark]. 183 | factory DartCodeViewer.designDark(String data) { 184 | return DartCodeViewer.textColor( 185 | data, 186 | baseColor: Colors.white, 187 | classColor: Color(0xFFFF8A80), 188 | commentColor: Color(0xFF607D8B), 189 | constantColor: Color(0xFF90A4AE), 190 | keywordColor: Color(0xFF26C6DA), 191 | numberColor: Color(0xFFFFBC00), 192 | punctuationColor: Color(0xFF90A4AE), 193 | stringColor: Color(0xFF00BFA4), 194 | backgroundColor: Color(0xFF263238), 195 | ); 196 | } 197 | 198 | /// Code viewer highlighter for Google IO 2017. 199 | factory DartCodeViewer.io17(String data) { 200 | return DartCodeViewer.textColor( 201 | data, 202 | baseColor: Colors.white, 203 | classColor: Color(0xFFFF8857), 204 | commentColor: Color(0xFFFF5CB4), 205 | constantColor: Color(0xFF90A4AE), 206 | keywordColor: Color(0xFF00E4FF), 207 | numberColor: Color(0xFFFFD500), 208 | punctuationColor: Color(0xFF90A4AE), 209 | stringColor: Color(0xFF1CE8b5), 210 | backgroundColor: Color(0xFF263238), 211 | ); 212 | } 213 | 214 | /// Code viewer highlighter for Google IO 2019. 215 | factory DartCodeViewer.io19(String data) { 216 | return DartCodeViewer.textColor( 217 | data, 218 | baseColor: Colors.white, 219 | classColor: Color(0xFFEE675C), 220 | commentColor: Color(0xFF9AA0A6), 221 | constantColor: Color(0xFFFCC934), 222 | keywordColor: Color(0xFF669DF6), 223 | numberColor: Color(0xFFFCC934), 224 | punctuationColor: Color(0xFF9AA0A6), 225 | stringColor: Color(0xFF5BB974), 226 | backgroundColor: Color(0xFF202124), 227 | ); 228 | } 229 | 230 | /// Code viewer highlighter for Flutter Interact 2019. 231 | factory DartCodeViewer.flutterInteract19(String data) { 232 | return DartCodeViewer.textColor( 233 | data, 234 | baseColor: Color(0xFFFAFBFB), 235 | classColor: Color(0xFFD65BAD), 236 | commentColor: Color(0xFF808080), 237 | constantColor: Color(0xFFFF8383), 238 | keywordColor: Color(0xFF1CDEC9), 239 | numberColor: Color(0xFFBD93F9), 240 | punctuationColor: Color(0xFF8BE9FD), 241 | stringColor: Color(0xFFffa65c), 242 | backgroundColor: Color(0xFF241e30), 243 | ); 244 | } 245 | 246 | /// The string that is transformed into code. This is a required variable. 247 | final String data; 248 | 249 | /// The text style for the plain text in code. 250 | final TextStyle? baseStyle; 251 | 252 | /// The text style for the code types in the code. 253 | /// 254 | /// For example: 255 | /// * The class name. 256 | /// * StatelessWidget and StatefulWidget. 257 | final TextStyle? classStyle; 258 | 259 | /// The text style for the commented out code. 260 | final TextStyle? commentStyle; 261 | 262 | /// The text style for the constant style code. 263 | final TextStyle? constantStyle; 264 | 265 | /// The text style for keywords. For example: 266 | /// * else 267 | /// * enum 268 | /// * export 269 | /// * external 270 | /// * factory 271 | /// * false 272 | final TextStyle? keywordStyle; 273 | 274 | /// The text style for numbers within the code. 275 | final TextStyle? numberStyle; 276 | 277 | /// The text style for punctuation code like periods and commas. 278 | final TextStyle? punctuationStyle; 279 | 280 | /// The text style for Strings. For example the data when using the [Text] widget. 281 | final TextStyle? stringStyle; 282 | 283 | /// The background Color of the code. By default it is [Theme.of(context).colorScheme.background]. 284 | final Color? backgroundColor; 285 | 286 | /// The text shown in the copy button by default it is 'COPY ALL'. 287 | final Text? copyButtonText; 288 | 289 | /// Shows copy button that lets user copy all the code as a raw string. By 290 | /// default the button is showing. 291 | final bool? showCopyButton; 292 | 293 | /// The height of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.height] 294 | final double? height; 295 | 296 | /// The width of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.width] 297 | final double? width; 298 | 299 | @override 300 | Widget build(BuildContext context) { 301 | final codeTextStyle = Theme.of(context).textTheme.bodyText1; 302 | 303 | final lightModeOn = Theme.of(context).brightness == Brightness.light; 304 | 305 | // These are defaults for the different types of text styles. The default 306 | // returns two different types of styles depending on the brightness of the 307 | // application. 308 | final _defaultBaseStyle = codeTextStyle?.copyWith( 309 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50, 310 | ); 311 | final _defaultClassStyle = codeTextStyle?.copyWith( 312 | color: lightModeOn ? Colors.purple.shade500 : Colors.purple.shade200, 313 | ); 314 | final _defaultCommentStyle = codeTextStyle?.copyWith( 315 | color: lightModeOn ? Colors.pink.shade600 : Colors.pink.shade300, 316 | ); 317 | final _defaultConstantStyle = codeTextStyle?.copyWith( 318 | color: lightModeOn ? Colors.indigo.shade500 : Colors.yellow.shade700, 319 | ); 320 | final _defaultKeywordStyle = codeTextStyle?.copyWith( 321 | color: lightModeOn ? Colors.indigo.shade500 : Colors.cyan.shade300, 322 | ); 323 | final _defaultNumberStyle = codeTextStyle?.copyWith( 324 | color: lightModeOn ? Colors.red.shade700 : Colors.yellow.shade700, 325 | ); 326 | final _defaultPunctuationalStyle = codeTextStyle?.copyWith( 327 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50, 328 | ); 329 | final _defaultStringStyle = codeTextStyle?.copyWith( 330 | color: lightModeOn ? Colors.green.shade700 : Colors.lightGreen.shade400, 331 | ); 332 | 333 | final _defaultCopyButtonText = Text('COPY ALL'); 334 | final _defaultShowCopyButton = true; 335 | 336 | var dartCodeViewerThemeData = DartCodeViewerTheme.of(context); 337 | dartCodeViewerThemeData = dartCodeViewerThemeData.copyWith( 338 | baseStyle: 339 | baseStyle ?? dartCodeViewerThemeData.baseStyle ?? _defaultBaseStyle, 340 | classStyle: classStyle ?? 341 | dartCodeViewerThemeData.classStyle ?? 342 | _defaultClassStyle, 343 | commentStyle: commentStyle ?? 344 | dartCodeViewerThemeData.commentStyle ?? 345 | _defaultCommentStyle, 346 | constantStyle: constantStyle ?? 347 | dartCodeViewerThemeData.constantStyle ?? 348 | _defaultConstantStyle, 349 | keywordStyle: keywordStyle ?? 350 | dartCodeViewerThemeData.keywordStyle ?? 351 | _defaultKeywordStyle, 352 | numberStyle: numberStyle ?? 353 | dartCodeViewerThemeData.numberStyle ?? 354 | _defaultNumberStyle, 355 | punctuationStyle: punctuationStyle ?? 356 | dartCodeViewerThemeData.punctuationStyle ?? 357 | _defaultPunctuationalStyle, 358 | stringStyle: stringStyle ?? 359 | dartCodeViewerThemeData.stringStyle ?? 360 | _defaultStringStyle, 361 | backgroundColor: backgroundColor ?? 362 | dartCodeViewerThemeData.backgroundColor ?? 363 | Theme.of(context).colorScheme.background, 364 | copyButtonText: copyButtonText ?? 365 | dartCodeViewerThemeData.copyButtonText ?? 366 | _defaultCopyButtonText, 367 | showCopyButton: showCopyButton ?? 368 | dartCodeViewerThemeData.showCopyButton ?? 369 | _defaultShowCopyButton, 370 | height: height ?? 371 | dartCodeViewerThemeData.height ?? 372 | MediaQuery.of(context).size.height, 373 | width: width ?? 374 | dartCodeViewerThemeData.width ?? 375 | MediaQuery.of(context).size.width, 376 | ); 377 | 378 | return DartCodeViewerTheme( 379 | data: dartCodeViewerThemeData, 380 | child: Container( 381 | color: dartCodeViewerThemeData.backgroundColor, 382 | padding: const EdgeInsets.symmetric(horizontal: 16), 383 | height: dartCodeViewerThemeData.height, 384 | width: dartCodeViewerThemeData.width, 385 | child: _DartCodeViewerPage( 386 | codifyString(data, dartCodeViewerThemeData), 387 | ), 388 | ), 389 | ); 390 | } 391 | 392 | InlineSpan codifyString( 393 | String content, 394 | DartCodeViewerThemeData dartCodeViewerThemeData, 395 | ) { 396 | final textSpans = []; 397 | final codeSpans = DartSyntaxPreHighlighter().format(content); 398 | // Converting CodeSpan to TextSpan by first converting to a string and then TextSpan. 399 | for (final span in codeSpans) { 400 | textSpans.add(stringToTextSpan(span.toString(), dartCodeViewerThemeData)); 401 | } 402 | return TextSpan(children: textSpans); 403 | } 404 | 405 | TextSpan stringToTextSpan( 406 | String string, 407 | DartCodeViewerThemeData dartCodeViewerThemeData, 408 | ) { 409 | return TextSpan( 410 | style: () { 411 | final String? styleString = 412 | RegExp(r'codeStyle.\w*').firstMatch(string)?.group(0); 413 | final dartCodeViewerTheme = dartCodeViewerThemeData; 414 | 415 | switch (styleString) { 416 | case 'codeStyle.baseStyle': 417 | return dartCodeViewerTheme.baseStyle; 418 | case 'codeStyle.numberStyle': 419 | return dartCodeViewerTheme.numberStyle; 420 | case 'codeStyle.commentStyle': 421 | return dartCodeViewerTheme.commentStyle; 422 | case 'codeStyle.keywordStyle': 423 | return dartCodeViewerTheme.keywordStyle; 424 | case 'codeStyle.stringStyle': 425 | return dartCodeViewerTheme.stringStyle; 426 | case 'codeStyle.punctuationStyle': 427 | return dartCodeViewerTheme.punctuationStyle; 428 | case 'codeStyle.classStyle': 429 | return dartCodeViewerTheme.classStyle; 430 | case 'codeStyle.constantStyle': 431 | return dartCodeViewerTheme.constantStyle; 432 | default: 433 | return dartCodeViewerTheme.baseStyle; 434 | } 435 | }(), 436 | text: () { 437 | final textString = RegExp('\'.*\'').firstMatch(string)?.group(0); 438 | final subString = textString!.substring(1, textString.length - 1); 439 | return decodeString(subString); 440 | }(), 441 | ); 442 | } 443 | 444 | /// Read raw string as regular String. Converts Unicode characters to actual 445 | /// numbers. 446 | String decodeString(String string) { 447 | return string 448 | .replaceAll(r'\u000a', '\n') 449 | .replaceAll(r'\u0027', '\'') 450 | .replaceAll(r'\u0009', '\t'); 451 | } 452 | } 453 | 454 | class _DartCodeViewerPage extends StatelessWidget { 455 | const _DartCodeViewerPage(this.code); 456 | final InlineSpan code; 457 | 458 | @override 459 | Widget build(BuildContext context) { 460 | final _richTextCode = code; 461 | final _plainTextCode = _richTextCode.toPlainText(); 462 | 463 | void _showSnackBarOnCopySuccess(dynamic result) { 464 | ScaffoldMessenger.of(context).showSnackBar( 465 | SnackBar( 466 | content: Text('Copied to Clipboard'), 467 | ), 468 | ); 469 | } 470 | 471 | void _showSnackBarOnCopyFailure(Object exception) { 472 | ScaffoldMessenger.of(context).showSnackBar( 473 | SnackBar( 474 | content: Text('Failure to copy to clipboard: $exception'), 475 | ), 476 | ); 477 | } 478 | 479 | return Column( 480 | crossAxisAlignment: CrossAxisAlignment.start, 481 | children: [ 482 | if (DartCodeViewerTheme.of(context).showCopyButton!) 483 | ElevatedButton( 484 | onPressed: () async { 485 | await Clipboard.setData(ClipboardData(text: _plainTextCode)) 486 | .then(_showSnackBarOnCopySuccess) 487 | .catchError(_showSnackBarOnCopyFailure); 488 | }, 489 | child: DartCodeViewerTheme.of(context).copyButtonText, 490 | ), 491 | Expanded( 492 | child: SingleChildScrollView( 493 | child: RichText( 494 | textDirection: TextDirection.ltr, 495 | text: _richTextCode, 496 | ), 497 | ), 498 | ), 499 | ], 500 | ); 501 | } 502 | } 503 | -------------------------------------------------------------------------------- /lib/src/dart_code_viewer_theme.dart: -------------------------------------------------------------------------------- 1 | import 'dart:ui'; 2 | 3 | import 'package:flutter/foundation.dart'; 4 | import 'package:flutter/material.dart'; 5 | 6 | /// Holds the color, size, and text styles for a dart code viewer theme. 7 | /// 8 | /// Use this class to configure a [DartCodeViewerThemeData] widget. 9 | /// 10 | /// To obtain the current ambient dart code viewer theme, use [DartCodeViewerTheme.of]. 11 | /// 12 | /// The parts of the dart code viewer are: 13 | /// * The 'data, which is the String that is supposed to be transformed into dart 14 | /// highlighted code. 15 | /// * The 'highlightedTextStyle' which the different type of highlighted code that 16 | /// can be highlighted differently. 17 | /// 18 | /// The simplest way to create a DartCodeThemeData is to use the [copyWith] on 19 | /// the one you get from [DartCodeViewerTheme.of], or create an entirely new one 20 | /// with [DartCodeViewerThemeData]. 21 | /// 22 | /// {@tool dartpad --template=stateless_widget_scaffold} 23 | /// 24 | /// Here is an example of a small string that shows up as Dart code in a flutter 25 | /// application. With [DartCodeViewerThemeData] applied 26 | /// 27 | /// ```dart 28 | /// @override 29 | /// Widget build(BuildContext context) { 30 | /// return DartCodeViewerTheme( 31 | /// data: DartCodeViewerThemeData( 32 | /// backgroundColor: Colors.pink, 33 | /// child: DartCodeViewer(r'class DartCodeViewer extends StatelessWidget {}'), 34 | /// ); 35 | /// ); 36 | /// } 37 | /// ``` 38 | /// {@end-tool} 39 | /// 40 | /// See also: 41 | /// * [DartCodeViewerThemeData], which describes the actual configuration of a 42 | /// [DartCodeViewerTheme] 43 | /// * [Code viewer online tool](https://romannurik.github.io/SlidesCodeHighlighter/) 44 | /// is a useful tool that lets you choose the color for each different style. 45 | /// On the left side you put your example code and on the right you can choose 46 | /// the colors you want the code viewer to display. 47 | /// background => backgroundColor 48 | /// plain text => baseStyle 49 | /// Punctuation => punctuationStyle 50 | /// String, values => stringStyle 51 | /// Keywords, tags => keywordStyle 52 | /// Comments => commentStyle 53 | /// Types => classStyle 54 | /// Numbers => numberStyle 55 | /// Declarations => constantStyle 56 | @immutable 57 | class DartCodeViewerThemeData with Diagnosticable { 58 | const DartCodeViewerThemeData({ 59 | this.baseStyle, 60 | this.classStyle, 61 | this.commentStyle, 62 | this.constantStyle, 63 | this.keywordStyle, 64 | this.numberStyle, 65 | this.punctuationStyle, 66 | this.stringStyle, 67 | this.backgroundColor, 68 | this.copyButtonText, 69 | this.showCopyButton, 70 | this.height, 71 | this.width, 72 | this.buttonStyle, 73 | }); 74 | 75 | /// The text style for the plain text in code. 76 | final TextStyle? baseStyle; 77 | 78 | /// The text style for the code types in the code. 79 | /// 80 | /// For example: 81 | /// * The class name. 82 | /// * StatelessWidget and StatefulWidget. 83 | final TextStyle? classStyle; 84 | 85 | /// The text style for the commented out code. 86 | final TextStyle? commentStyle; 87 | 88 | /// The text style for the constant style code. 89 | final TextStyle? constantStyle; 90 | 91 | /// The text style for keywords. For example: 92 | /// * else 93 | /// * enum 94 | /// * export 95 | /// * external 96 | /// * factory 97 | /// * false 98 | final TextStyle? keywordStyle; 99 | 100 | /// The text style for numbers within the code. 101 | final TextStyle? numberStyle; 102 | 103 | /// The text style for punctuation code like periods and commas. 104 | final TextStyle? punctuationStyle; 105 | 106 | /// The text style for Strings. For example the data when using the [Text] widget. 107 | final TextStyle? stringStyle; 108 | 109 | /// The background Color of the code. By default it is [Theme.of(context).colorScheme.background]. 110 | final Color? backgroundColor; 111 | 112 | /// The text shown in the copy button by default it is 'COPY ALL'. 113 | final Text? copyButtonText; 114 | 115 | /// Shows copy button that lets user copy all the code as a raw string. By 116 | /// default the button is showing. 117 | final bool? showCopyButton; 118 | 119 | /// The height of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.height] 120 | final double? height; 121 | 122 | /// The width of the [DartCodeViewer] by default it uses the [MediaQuery.of(context).size.width] 123 | final double? width; 124 | 125 | /// The buttonThemeData for the [DartCodeViewer]. Useful if you want the copy 126 | /// button to appear differently than the default. 127 | final ButtonStyle? buttonStyle; 128 | 129 | /// Creates a copy of this object but with the given fields replaced with the 130 | /// new values. 131 | DartCodeViewerThemeData copyWith({ 132 | TextStyle? baseStyle, 133 | TextStyle? classStyle, 134 | TextStyle? commentStyle, 135 | TextStyle? constantStyle, 136 | TextStyle? keywordStyle, 137 | TextStyle? numberStyle, 138 | TextStyle? punctuationStyle, 139 | TextStyle? stringStyle, 140 | Color? backgroundColor, 141 | Text? copyButtonText, 142 | bool? showCopyButton, 143 | double? height, 144 | double? width, 145 | ButtonStyle? buttonStyle, 146 | }) { 147 | return DartCodeViewerThemeData( 148 | baseStyle: baseStyle ?? this.baseStyle, 149 | classStyle: classStyle ?? this.classStyle, 150 | commentStyle: commentStyle ?? this.commentStyle, 151 | constantStyle: constantStyle ?? this.constantStyle, 152 | keywordStyle: keywordStyle ?? this.keywordStyle, 153 | numberStyle: numberStyle ?? this.numberStyle, 154 | punctuationStyle: punctuationStyle ?? this.punctuationStyle, 155 | stringStyle: stringStyle ?? this.stringStyle, 156 | backgroundColor: backgroundColor ?? this.backgroundColor, 157 | copyButtonText: copyButtonText ?? this.copyButtonText, 158 | showCopyButton: showCopyButton ?? this.showCopyButton, 159 | height: height ?? this.height, 160 | width: width ?? this.width, 161 | ); 162 | } 163 | 164 | /// Linearly interpolate between two dart code viewer themes. 165 | /// 166 | /// The arguments must not be null. 167 | static DartCodeViewerThemeData lerp( 168 | DartCodeViewerThemeData a, 169 | DartCodeViewerThemeData b, 170 | double t, 171 | ) { 172 | return DartCodeViewerThemeData( 173 | baseStyle: TextStyle.lerp(a.baseStyle, b.baseStyle, t), 174 | classStyle: TextStyle.lerp(a.classStyle, b.classStyle, t), 175 | commentStyle: TextStyle.lerp(a.commentStyle, b.commentStyle, t), 176 | constantStyle: TextStyle.lerp(a.constantStyle, b.constantStyle, t), 177 | keywordStyle: TextStyle.lerp(a.keywordStyle, b.keywordStyle, t), 178 | numberStyle: TextStyle.lerp(a.numberStyle, b.numberStyle, t), 179 | punctuationStyle: TextStyle.lerp( 180 | a.punctuationStyle, 181 | b.punctuationStyle, 182 | t, 183 | ), 184 | stringStyle: TextStyle.lerp(a.stringStyle, b.stringStyle, t), 185 | backgroundColor: Color.lerp(a.backgroundColor, b.backgroundColor, t), 186 | height: lerpDouble(a.height, b.height, t), 187 | width: lerpDouble(a.width, b.width, t), 188 | ); 189 | } 190 | 191 | @override 192 | int get hashCode { 193 | return hashValues( 194 | baseStyle, 195 | classStyle, 196 | commentStyle, 197 | constantStyle, 198 | keywordStyle, 199 | numberStyle, 200 | punctuationStyle, 201 | stringStyle, 202 | backgroundColor, 203 | copyButtonText, 204 | showCopyButton, 205 | height, 206 | width, 207 | ); 208 | } 209 | 210 | @override 211 | bool operator ==(Object other) { 212 | if (identical(this, other)) { 213 | return true; 214 | } 215 | if (runtimeType != other.runtimeType) { 216 | return false; 217 | } 218 | return other is DartCodeViewerThemeData && 219 | baseStyle == other.baseStyle && 220 | classStyle == other.classStyle && 221 | commentStyle == other.commentStyle && 222 | constantStyle == other.constantStyle && 223 | keywordStyle == other.keywordStyle && 224 | numberStyle == other.numberStyle && 225 | punctuationStyle == other.punctuationStyle && 226 | stringStyle == other.stringStyle && 227 | backgroundColor == other.backgroundColor && 228 | copyButtonText == other.copyButtonText && 229 | showCopyButton == other.showCopyButton && 230 | height == other.height && 231 | width == other.width; 232 | } 233 | 234 | @override 235 | void debugFillProperties(DiagnosticPropertiesBuilder properties) { 236 | super.debugFillProperties(properties); 237 | properties.add( 238 | DiagnosticsProperty( 239 | 'baseStyle', 240 | baseStyle, 241 | defaultValue: null, 242 | ), 243 | ); 244 | properties.add( 245 | DiagnosticsProperty( 246 | 'classStyle', 247 | classStyle, 248 | defaultValue: null, 249 | ), 250 | ); 251 | properties.add( 252 | DiagnosticsProperty( 253 | 'commentStyle', 254 | commentStyle, 255 | defaultValue: null, 256 | ), 257 | ); 258 | properties.add( 259 | DiagnosticsProperty( 260 | 'constantStyle', 261 | constantStyle, 262 | defaultValue: null, 263 | ), 264 | ); 265 | properties.add( 266 | DiagnosticsProperty( 267 | 'keywordStyle', 268 | keywordStyle, 269 | defaultValue: null, 270 | ), 271 | ); 272 | properties.add( 273 | DiagnosticsProperty( 274 | 'numberStyle', 275 | numberStyle, 276 | defaultValue: null, 277 | ), 278 | ); 279 | properties.add( 280 | DiagnosticsProperty( 281 | 'punctuationStyle', 282 | punctuationStyle, 283 | defaultValue: null, 284 | ), 285 | ); 286 | properties.add( 287 | DiagnosticsProperty( 288 | 'stringStyle', 289 | stringStyle, 290 | defaultValue: null, 291 | ), 292 | ); 293 | properties.add( 294 | DiagnosticsProperty( 295 | 'stringStyle', 296 | stringStyle, 297 | defaultValue: null, 298 | ), 299 | ); 300 | properties.add( 301 | DiagnosticsProperty( 302 | 'backgroundColor', 303 | backgroundColor, 304 | defaultValue: null, 305 | ), 306 | ); 307 | properties.add( 308 | DiagnosticsProperty( 309 | 'copyButtonText', 310 | copyButtonText, 311 | defaultValue: null, 312 | ), 313 | ); 314 | properties.add( 315 | DiagnosticsProperty( 316 | 'showCopyButton', 317 | showCopyButton, 318 | defaultValue: null, 319 | ), 320 | ); 321 | properties.add( 322 | DiagnosticsProperty( 323 | 'height', 324 | height, 325 | defaultValue: null, 326 | ), 327 | ); 328 | properties.add( 329 | DiagnosticsProperty( 330 | 'width', 331 | width, 332 | defaultValue: null, 333 | ), 334 | ); 335 | } 336 | } 337 | 338 | /// Applies a DartCodeViewerTheme to [DartCodeViewer]. 339 | /// 340 | /// The dart code viewer theme describes the color, size, and text styles for the 341 | /// dart code viewer it is attached to. 342 | /// 343 | /// Descendant widget obtains the current theme's [DartCodeViewerThemeData] 344 | /// object using [DartCodeViewerTheme.of]. When a widget uses 345 | /// [DartCodeViewerTheme.of], it is automatically rebuilt if the theme later 346 | /// changes. 347 | /// 348 | /// See also: 349 | /// * [DartCodeViewer], a code viewer for the dart language. 350 | /// * [DartCodeViewerThemeData], which describes the actual configuration of a 351 | /// dart code viewer theme. 352 | /// * [ThemeData], which describes the overall theme information for the 353 | /// application. 354 | class DartCodeViewerTheme extends InheritedTheme { 355 | /// Applies the given theme [data] to [child]. 356 | /// 357 | /// The [data] and [child] arguments must not be null. 358 | const DartCodeViewerTheme({ 359 | Key? key, 360 | required this.data, 361 | required Widget child, 362 | }) : super(key: key, child: child); 363 | 364 | /// Specifies the color, size, and text styles for the descendant dart code 365 | /// viewer widgets. 366 | final DartCodeViewerThemeData data; 367 | 368 | /// Returns the data from the closest [DartCodeViewerTheme] instances the 369 | /// encloses the given context. 370 | /// 371 | /// The default parameters is set within the [DartCodeViewer]. 372 | /// 373 | /// {@tool dartpad --template=stateless_widget_scaffold} 374 | /// 375 | /// Here is an example of a small string that shows up as Dart code in a flutter 376 | /// application. With [DartCodeViewerThemeData] applied 377 | /// 378 | /// ```dart 379 | /// @override 380 | /// Widget build(BuildContext context) { 381 | /// return DartCodeViewerTheme( 382 | /// data: DartCodeViewerThemeData( 383 | /// backgroundColor: Colors.pink, 384 | /// child: DartCodeViewer(r'class DartCodeViewer extends StatelessWidget {}'), 385 | /// ); 386 | /// ); 387 | /// } 388 | /// ``` 389 | /// {@end-tool} 390 | /// 391 | /// See also: 392 | /// * [DartCodeViewerThemeData], which describes the actual configuration of a 393 | /// [DartCodeViewerTheme] 394 | /// * [Code viewer online tool](https://romannurik.github.io/SlidesCodeHighlighter/) 395 | /// is a useful tool that lets you choose the color for each different style. 396 | /// On the left side you put your example code and on the right you can choose 397 | /// the colors you want the code viewer to display. 398 | /// background => backgroundColor 399 | /// plain text => baseStyle 400 | /// Punctuation => punctuationStyle 401 | /// String, values => stringStyle 402 | /// Keywords, tags => keywordStyle 403 | /// Comments => commentStyle 404 | /// Types => classStyle 405 | /// Numbers => numberStyle 406 | /// Declarations => constantStyle 407 | static DartCodeViewerThemeData of(BuildContext context) { 408 | final dartCodeViewerTheme = 409 | context.dependOnInheritedWidgetOfExactType(); 410 | return dartCodeViewerTheme?.data ?? DartCodeViewerThemeData(); 411 | } 412 | 413 | @override 414 | Widget wrap(BuildContext context, Widget child) { 415 | final ancestorTheme = 416 | context.findAncestorWidgetOfExactType(); 417 | return identical(this, ancestorTheme) 418 | ? child 419 | : DartCodeViewerTheme(data: data, child: child); 420 | } 421 | 422 | @override 423 | bool updateShouldNotify(DartCodeViewerTheme oldWidget) => 424 | data != oldWidget.data; 425 | } 426 | -------------------------------------------------------------------------------- /lib/src/pre_highlighter.dart: -------------------------------------------------------------------------------- 1 | import 'package:string_scanner/string_scanner.dart'; 2 | 3 | abstract class SyntaxPreHighlighter { 4 | List format(String src); 5 | } 6 | 7 | class DartSyntaxPreHighlighter extends SyntaxPreHighlighter { 8 | DartSyntaxPreHighlighter() { 9 | _spans = <_HighlightSpan>[]; 10 | } 11 | 12 | static const List _keywords = [ 13 | 'abstract', 14 | 'as', 15 | 'assert', 16 | 'async', 17 | 'await', 18 | 'break', 19 | 'case', 20 | 'catch', 21 | 'class', 22 | 'const', 23 | 'continue', 24 | 'default', 25 | 'deferred', 26 | 'do', 27 | 'dynamic', 28 | 'else', 29 | 'enum', 30 | 'export', 31 | 'external', 32 | 'extends', 33 | 'factory', 34 | 'false', 35 | 'final', 36 | 'finally', 37 | 'for', 38 | 'get', 39 | 'if', 40 | 'implements', 41 | 'import', 42 | 'in', 43 | 'is', 44 | 'library', 45 | 'new', 46 | 'null', 47 | 'operator', 48 | 'part', 49 | 'rethrow', 50 | 'return', 51 | 'set', 52 | 'static', 53 | 'super', 54 | 'switch', 55 | 'sync', 56 | 'this', 57 | 'throw', 58 | 'true', 59 | 'try', 60 | 'typedef', 61 | 'var', 62 | 'void', 63 | 'while', 64 | 'with', 65 | 'yield', 66 | ]; 67 | 68 | static const List _builtInTypes = [ 69 | 'int', 70 | 'double', 71 | 'num', 72 | 'bool', 73 | ]; 74 | 75 | late String _src; 76 | late StringScanner _scanner; 77 | 78 | late List<_HighlightSpan> _spans; 79 | 80 | @override 81 | List format(String src) { 82 | _src = src; 83 | _scanner = StringScanner(_src); 84 | 85 | if (_generateSpans()) { 86 | // Successfully parsed the code 87 | final formattedText = []; 88 | var currentPosition = 0; 89 | 90 | for (final span in _spans) { 91 | if (currentPosition != span.start) { 92 | formattedText.add( 93 | CodeSpan( 94 | text: _src.substring(currentPosition, span.start), 95 | ), 96 | ); 97 | } 98 | 99 | formattedText.add( 100 | CodeSpan( 101 | type: span.type, 102 | text: span.textForSpan(_src), 103 | ), 104 | ); 105 | 106 | currentPosition = span.end; 107 | } 108 | 109 | if (currentPosition != _src.length) { 110 | formattedText.add( 111 | CodeSpan( 112 | text: _src.substring(currentPosition, _src.length), 113 | ), 114 | ); 115 | } 116 | 117 | return formattedText; 118 | } else { 119 | // Parsing failed, return with only basic formatting 120 | return [CodeSpan(type: _HighlightType.base, text: src)]; 121 | } 122 | } 123 | 124 | bool _generateSpans() { 125 | var lastLoopPosition = _scanner.position; 126 | 127 | while (!_scanner.isDone) { 128 | // Skip White space 129 | _scanner.scan(RegExp(r'\s+')); 130 | 131 | // Block comments 132 | if (_scanner.scan(RegExp(r'/\*(.|\n)*\*/'))) { 133 | _spans.add(_HighlightSpan( 134 | _HighlightType.comment, 135 | _scanner.lastMatch!.start, 136 | _scanner.lastMatch!.end, 137 | )); 138 | continue; 139 | } 140 | 141 | // Line comments 142 | if (_scanner.scan('//')) { 143 | final startComment = _scanner.lastMatch!.start; 144 | 145 | var eof = false; 146 | int endComment; 147 | if (_scanner.scan(RegExp(r'.*\n'))) { 148 | endComment = _scanner.lastMatch!.end - 1; 149 | } else { 150 | eof = true; 151 | endComment = _src.length; 152 | } 153 | 154 | _spans.add(_HighlightSpan( 155 | _HighlightType.comment, 156 | startComment, 157 | endComment, 158 | )); 159 | 160 | if (eof) { 161 | break; 162 | } 163 | 164 | continue; 165 | } 166 | 167 | // Raw r"String" 168 | if (_scanner.scan(RegExp(r'r".*"'))) { 169 | _spans.add(_HighlightSpan( 170 | _HighlightType.string, 171 | _scanner.lastMatch!.start, 172 | _scanner.lastMatch!.end, 173 | )); 174 | continue; 175 | } 176 | 177 | // Raw r'String' 178 | if (_scanner.scan(RegExp(r"r'.*'"))) { 179 | _spans.add(_HighlightSpan( 180 | _HighlightType.string, 181 | _scanner.lastMatch!.start, 182 | _scanner.lastMatch!.end, 183 | )); 184 | continue; 185 | } 186 | 187 | // Multiline """String""" 188 | if (_scanner.scan(RegExp(r'"""(?:[^"\\]|\\(.|\n))*"""'))) { 189 | _spans.add(_HighlightSpan( 190 | _HighlightType.string, 191 | _scanner.lastMatch!.start, 192 | _scanner.lastMatch!.end, 193 | )); 194 | continue; 195 | } 196 | 197 | // Multiline '''String''' 198 | if (_scanner.scan(RegExp(r"'''(?:[^'\\]|\\(.|\n))*'''"))) { 199 | _spans.add(_HighlightSpan( 200 | _HighlightType.string, 201 | _scanner.lastMatch!.start, 202 | _scanner.lastMatch!.end, 203 | )); 204 | continue; 205 | } 206 | 207 | // "String" 208 | if (_scanner.scan(RegExp(r'"(?:[^"\\]|\\.)*"'))) { 209 | _spans.add(_HighlightSpan( 210 | _HighlightType.string, 211 | _scanner.lastMatch!.start, 212 | _scanner.lastMatch!.end, 213 | )); 214 | continue; 215 | } 216 | 217 | // 'String' 218 | if (_scanner.scan(RegExp(r"'(?:[^'\\]|\\.)*'"))) { 219 | _spans.add(_HighlightSpan( 220 | _HighlightType.string, 221 | _scanner.lastMatch!.start, 222 | _scanner.lastMatch!.end, 223 | )); 224 | continue; 225 | } 226 | 227 | // Double 228 | if (_scanner.scan(RegExp(r'\d+\.\d+'))) { 229 | _spans.add(_HighlightSpan( 230 | _HighlightType.number, 231 | _scanner.lastMatch!.start, 232 | _scanner.lastMatch!.end, 233 | )); 234 | continue; 235 | } 236 | 237 | // Integer 238 | if (_scanner.scan(RegExp(r'\d+'))) { 239 | _spans.add( 240 | _HighlightSpan( 241 | _HighlightType.number, 242 | _scanner.lastMatch!.start, 243 | _scanner.lastMatch!.end, 244 | ), 245 | ); 246 | continue; 247 | } 248 | 249 | // Punctuation 250 | if (_scanner.scan(RegExp(r'[\[\]{}().!=<>&\|\?\+\-\*/%\^~;:,]'))) { 251 | _spans.add(_HighlightSpan( 252 | _HighlightType.punctuation, 253 | _scanner.lastMatch!.start, 254 | _scanner.lastMatch!.end, 255 | )); 256 | continue; 257 | } 258 | 259 | // Meta data 260 | if (_scanner.scan(RegExp(r'@\w+'))) { 261 | _spans.add(_HighlightSpan( 262 | _HighlightType.keyword, 263 | _scanner.lastMatch!.start, 264 | _scanner.lastMatch!.end, 265 | )); 266 | continue; 267 | } 268 | 269 | // Words 270 | if (_scanner.scan(RegExp(r'\w+'))) { 271 | late _HighlightType type; 272 | 273 | var word = _scanner.lastMatch![0]; 274 | if (word!.startsWith('_')) { 275 | word = word.substring(1); 276 | } 277 | 278 | if (_keywords.contains(word)) { 279 | type = _HighlightType.keyword; 280 | } else if (_builtInTypes.contains(word)) { 281 | type = _HighlightType.keyword; 282 | } else if (_firstLetterIsUpperCase(word)) { 283 | type = _HighlightType.klass; 284 | } else if (word.length >= 2 && 285 | word.startsWith('k') && 286 | _firstLetterIsUpperCase(word.substring(1))) { 287 | type = _HighlightType.constant; 288 | } else { 289 | type = _HighlightType.base; 290 | } 291 | 292 | _spans.add(_HighlightSpan( 293 | type, 294 | _scanner.lastMatch!.start, 295 | _scanner.lastMatch!.end, 296 | )); 297 | } 298 | 299 | // Check if this loop did anything 300 | if (lastLoopPosition == _scanner.position) { 301 | // Failed to parse this file, abort gracefully 302 | return false; 303 | } 304 | lastLoopPosition = _scanner.position; 305 | } 306 | 307 | _simplify(); 308 | return true; 309 | } 310 | 311 | void _simplify() { 312 | for (var i = _spans.length - 2; i >= 0; i -= 1) { 313 | if (_spans[i].type == _spans[i + 1].type && 314 | _spans[i].end == _spans[i + 1].start) { 315 | _spans[i] = _HighlightSpan( 316 | _spans[i].type, 317 | _spans[i].start, 318 | _spans[i + 1].end, 319 | ); 320 | _spans.removeAt(i + 1); 321 | } 322 | } 323 | } 324 | 325 | bool _firstLetterIsUpperCase(String str) { 326 | if (str.isNotEmpty) { 327 | final first = str.substring(0, 1); 328 | return first == first.toUpperCase(); 329 | } 330 | return false; 331 | } 332 | } 333 | 334 | enum _HighlightType { 335 | number, 336 | comment, 337 | keyword, 338 | string, 339 | punctuation, 340 | klass, 341 | constant, 342 | base, 343 | } 344 | 345 | class _HighlightSpan { 346 | _HighlightSpan(this.type, this.start, this.end); 347 | final _HighlightType type; 348 | final int start; 349 | final int end; 350 | 351 | String textForSpan(String src) { 352 | return src.substring(start, end); 353 | } 354 | } 355 | 356 | class CodeSpan { 357 | CodeSpan({this.type = _HighlightType.base, required this.text}); 358 | 359 | final _HighlightType type; 360 | final String text; 361 | 362 | @override 363 | String toString() { 364 | return 'TextSpan(' 365 | 'style: codeStyle.${_styleNameOf(type)}, ' 366 | "text: '${escape(text)}'" 367 | ')'; 368 | } 369 | } 370 | 371 | String _styleNameOf(_HighlightType type) { 372 | switch (type) { 373 | case _HighlightType.number: 374 | return 'numberStyle'; 375 | case _HighlightType.comment: 376 | return 'commentStyle'; 377 | case _HighlightType.keyword: 378 | return 'keywordStyle'; 379 | case _HighlightType.string: 380 | return 'stringStyle'; 381 | case _HighlightType.punctuation: 382 | return 'punctuationStyle'; 383 | case _HighlightType.klass: 384 | return 'classStyle'; 385 | case _HighlightType.constant: 386 | return 'constantStyle'; 387 | case _HighlightType.base: 388 | return 'baseStyle'; 389 | } 390 | } 391 | 392 | String escape(String text) { 393 | final escapedText = StringBuffer(); 394 | 395 | for (final char in text.runes) { 396 | if (char < 0x20 || 397 | char >= 0x7F || 398 | char == 0x22 || 399 | char == 0x24 || 400 | char == 0x27 || 401 | char == 0x5C) { 402 | if (char <= 0xffff) { 403 | escapedText.write('\\u${_encodeAndPad(char)}'); 404 | } else { 405 | escapedText.write('\\u{${_encode(char)}}'); 406 | } 407 | } else { 408 | escapedText.write(String.fromCharCode(char)); 409 | } 410 | } 411 | 412 | return escapedText.toString(); 413 | } 414 | 415 | String _encode(int charCode) { 416 | return charCode.toRadixString(16); 417 | } 418 | 419 | String _encodeAndPad(int charCode) { 420 | final encoded = _encode(charCode); 421 | return '0' * (4 - encoded.length) + encoded; 422 | } 423 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: dart_code_viewer 2 | description: A package to view Dart Code in your Flutter application. 3 | version: 0.0.2 4 | homepage: https://github.com/JoseAlba/dart-code-viewer/ 5 | 6 | environment: 7 | sdk: ">=2.12.0-0 <3.0.0" 8 | 9 | dependencies: 10 | flutter: 11 | sdk: flutter 12 | string_scanner: ^1.0.5 13 | 14 | dev_dependencies: 15 | flutter_test: 16 | sdk: flutter 17 | -------------------------------------------------------------------------------- /test/dart_code_viewer_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:dart_code_viewer/dart_code_viewer.dart'; 4 | 5 | void main() { 6 | String data = r''' 7 | import 'package:flutter/material.dart'; 8 | import 'package:flutter/services.dart'; 9 | import 'package:google_fonts/google_fonts.dart'; 10 | import 'dart_code_viewer_theme.dart'; 11 | import 'pre_highlighter.dart'; 12 | 13 | class DartCodeViewer extends StatelessWidget { 14 | const DartCodeViewer( 15 | this.data, { 16 | Key key, 17 | this.baseStyle, 18 | this.classStyle, 19 | this.commentStyle, 20 | this.constantStyle, 21 | this.keywordStyle, 22 | this.numberStyle, 23 | this.punctuationStyle, 24 | this.stringStyle, 25 | this.backgroundColor, 26 | this.copyButtonText, 27 | this.showCopyButton, 28 | this.height, 29 | this.width, 30 | }) : assert(data != null, 'A non-null String must be provided to a DartCodeViewer widget.'), 31 | super(key: key); 32 | 33 | factory DartCodeViewer.textColor( 34 | String data, { 35 | TextStyle textStyle, 36 | Color baseColor, 37 | Color classColor, 38 | Color commentColor, 39 | Color constantColor, 40 | Color keywordColor, 41 | Color numberColor, 42 | Color punctuationColor, 43 | Color stringColor, 44 | Color backgroundColor, 45 | Text copyButtonText, 46 | bool showCopyButton, 47 | double height, 48 | double width, 49 | }) { 50 | TextStyle _defaultCodeStyle = textStyle ?? GoogleFonts.robotoMono(fontSize: 12); 51 | return DartCodeViewer( 52 | data, 53 | baseStyle: _defaultCodeStyle.copyWith(color: baseColor), 54 | classStyle: _defaultCodeStyle.copyWith(color: classColor), 55 | commentStyle: _defaultCodeStyle.copyWith(color: commentColor), 56 | constantStyle: _defaultCodeStyle.copyWith(color: constantColor), 57 | keywordStyle: _defaultCodeStyle.copyWith(color: keywordColor), 58 | numberStyle: _defaultCodeStyle.copyWith(color: numberColor), 59 | punctuationStyle: _defaultCodeStyle.copyWith(color: punctuationColor), 60 | stringStyle: _defaultCodeStyle.copyWith(color: stringColor), 61 | backgroundColor: backgroundColor, 62 | copyButtonText: copyButtonText, 63 | showCopyButton: showCopyButton, 64 | height: height, 65 | width: width, 66 | ); 67 | } 68 | 69 | factory DartCodeViewer.light(String data) { 70 | return DartCodeViewer.textColor( 71 | data, 72 | baseColor: Colors.blueGrey.shade800, 73 | classColor: Colors.purple.shade500, 74 | commentColor: Colors.pink.shade600, 75 | constantColor: Colors.indigo.shade50, 76 | keywordColor: Colors.indigo.shade500, 77 | numberColor: Colors.red.shade700, 78 | punctuationColor: Colors.blueGrey.shade800, 79 | stringColor: Colors.green.shade700, 80 | backgroundColor: Colors.grey.shade100, 81 | ); 82 | } 83 | 84 | factory DartCodeViewer.lightAlt(String data) { 85 | return DartCodeViewer.textColor( 86 | data, 87 | baseColor: Colors.black, 88 | classColor: Color(0xFF673AB7), 89 | commentColor: Color(0xFF999999), 90 | constantColor: Color(0xFFE67C73), 91 | keywordColor: Color(0xFF4285F4), 92 | numberColor: Color(0xFFDB4437), 93 | punctuationColor: Color(0xFFA3A3A3), 94 | stringColor: Color(0xFF0F9D58), 95 | backgroundColor: Color(0xFFEEEEEE), 96 | ); 97 | } 98 | 99 | factory DartCodeViewer.dark(String data) { 100 | return DartCodeViewer.textColor( 101 | data, 102 | baseColor: Colors.blueGrey.shade50, 103 | classColor: Colors.purple.shade200, 104 | commentColor: Colors.pink.shade300, 105 | constantColor: Colors.yellow.shade700, 106 | keywordColor: Colors.cyan.shade300, 107 | numberColor: Colors.yellow.shade700, 108 | punctuationColor: Colors.blueGrey.shade50, 109 | stringColor: Colors.lightGreen.shade400, 110 | backgroundColor: Colors.grey.shade900, 111 | ); 112 | } 113 | 114 | factory DartCodeViewer.darkAlt(String data) { 115 | return DartCodeViewer.textColor( 116 | data, 117 | baseColor: Colors.white, 118 | classColor: Color(0xFFFF8A65), 119 | commentColor: Color(0xFFAAAAAA), 120 | constantColor: Color(0xFFE67C73), 121 | keywordColor: Color(0xFF7BAAF7), 122 | numberColor: Color(0xFFF4B400), 123 | punctuationColor: Color(0xFFA3A3A3), 124 | stringColor: Color(0xFF57BB8A), 125 | backgroundColor: Color(0xFF000000), 126 | ); 127 | } 128 | 129 | factory DartCodeViewer.designDark(String data) { 130 | return DartCodeViewer.textColor( 131 | data, 132 | baseColor: Colors.white, 133 | classColor: Color(0xFFFF8A80), 134 | commentColor: Color(0xFF607D8B), 135 | constantColor: Color(0xFF90A4AE), 136 | keywordColor: Color(0xFF26C6DA), 137 | numberColor: Color(0xFFFFBC00), 138 | punctuationColor: Color(0xFF90A4AE), 139 | stringColor: Color(0xFF00BFA4), 140 | backgroundColor: Color(0xFF263238), 141 | ); 142 | } 143 | 144 | factory DartCodeViewer.io17(String data) { 145 | return DartCodeViewer.textColor( 146 | data, 147 | baseColor: Colors.white, 148 | classColor: Color(0xFFFF8857), 149 | commentColor: Color(0xFFFF5CB4), 150 | constantColor: Color(0xFF90A4AE), 151 | keywordColor: Color(0xFF00E4FF), 152 | numberColor: Color(0xFFFFD500), 153 | punctuationColor: Color(0xFF90A4AE), 154 | stringColor: Color(0xFF1CE8b5), 155 | backgroundColor: Color(0xFF263238), 156 | ); 157 | } 158 | 159 | factory DartCodeViewer.io19(String data) { 160 | return DartCodeViewer.textColor( 161 | data, 162 | baseColor: Colors.white, 163 | classColor: Color(0xFFEE675C), 164 | commentColor: Color(0xFF9AA0A6), 165 | constantColor: Color(0xFFFCC934), 166 | keywordColor: Color(0xFF669DF6), 167 | numberColor: Color(0xFFFCC934), 168 | punctuationColor: Color(0xFF9AA0A6), 169 | stringColor: Color(0xFF5BB974), 170 | backgroundColor: Color(0xFF202124), 171 | ); 172 | } 173 | 174 | factory DartCodeViewer.flutterInteract19(String data) { 175 | return DartCodeViewer.textColor( 176 | data, 177 | baseColor: Color(0xFFFAFBFB), 178 | classColor: Color(0xFFD65BAD), 179 | commentColor: Color(0xFF808080), 180 | constantColor: Color(0xFFFF8383), 181 | keywordColor: Color(0xFF1CDEC9), 182 | numberColor: Color(0xFFBD93F9), 183 | punctuationColor: Color(0xFF8BE9FD), 184 | stringColor: Color(0xFFffa65c), 185 | backgroundColor: Color(0xFF241e30), 186 | ); 187 | } 188 | 189 | final TextStyle baseStyle; 190 | final TextStyle classStyle; 191 | final TextStyle commentStyle; 192 | final TextStyle constantStyle; 193 | final TextStyle keywordStyle; 194 | final TextStyle numberStyle; 195 | final TextStyle punctuationStyle; 196 | final TextStyle stringStyle; 197 | final String data; 198 | 199 | final Color backgroundColor; 200 | final Text copyButtonText; 201 | final bool showCopyButton; 202 | 203 | final double height; 204 | final double width; 205 | 206 | @override 207 | Widget build(BuildContext context) { 208 | final codeTextStyle = GoogleFonts.robotoMono( 209 | fontSize: 12 * MediaQuery.of(context).textScaleFactor, 210 | ); 211 | 212 | /// Uses device brightness to choose what type of theme to return. 213 | final brightness = WidgetsBinding.instance.window.platformBrightness; 214 | bool lightModeOn = brightness == Brightness.light; 215 | 216 | TextStyle _defaultBaseStyle = codeTextStyle.copyWith( 217 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50 218 | ); 219 | TextStyle _defaultClassStyle = codeTextStyle.copyWith( 220 | color: lightModeOn ? Colors.purple.shade500 : Colors.purple.shade200 221 | ); 222 | TextStyle _defaultCommentStyle = codeTextStyle.copyWith( 223 | color: lightModeOn ? Colors.pink.shade600 : Colors.pink.shade300 224 | ); 225 | TextStyle _defaultConstantStyle = codeTextStyle.copyWith( 226 | color: lightModeOn ? Colors.indigo.shade50 : Colors.yellow.shade700 227 | ); 228 | TextStyle _defaultKeywordStyle = codeTextStyle.copyWith( 229 | color: lightModeOn ? Colors.indigo.shade500 : Colors.cyan.shade300 230 | ); 231 | TextStyle _defaultNumberStyle = codeTextStyle.copyWith( 232 | color: lightModeOn ? Colors.red.shade700 : Colors.yellow.shade700 233 | ); 234 | TextStyle _defaultPunctuationalStyle = codeTextStyle.copyWith( 235 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50 236 | ); 237 | TextStyle _defaultStringStyle = codeTextStyle.copyWith( 238 | color: lightModeOn ? Colors.green.shade700 : Colors.lightGreen.shade400 239 | ); 240 | 241 | Text _defaultCopyButtonText = Text('' 242 | 'COPY ALL', 243 | style: Theme.of(context).textTheme.button.copyWith( 244 | color: Colors.white, 245 | fontWeight: FontWeight.w500, 246 | ), 247 | ); 248 | bool _defaultShowCopyButton = true; 249 | 250 | DartCodeViewerThemeData dartCodeViewerThemeData = DartCodeViewerTheme.of(context); 251 | dartCodeViewerThemeData = dartCodeViewerThemeData.copyWith( 252 | baseStyle: baseStyle ?? dartCodeViewerThemeData.baseStyle ?? _defaultBaseStyle, 253 | classStyle: classStyle ?? dartCodeViewerThemeData.classStyle ?? _defaultClassStyle, 254 | commentStyle: commentStyle ?? dartCodeViewerThemeData.commentStyle ?? _defaultCommentStyle, 255 | constantStyle: constantStyle ?? dartCodeViewerThemeData.constantStyle ?? _defaultConstantStyle, 256 | keywordStyle: keywordStyle ?? dartCodeViewerThemeData.keywordStyle ?? _defaultKeywordStyle, 257 | numberStyle: numberStyle ?? dartCodeViewerThemeData.numberStyle ?? _defaultNumberStyle, 258 | punctuationStyle: punctuationStyle ?? dartCodeViewerThemeData.punctuationStyle ?? _defaultPunctuationalStyle, 259 | stringStyle: stringStyle ?? dartCodeViewerThemeData.stringStyle ?? _defaultStringStyle, 260 | backgroundColor: backgroundColor ?? dartCodeViewerThemeData.backgroundColor ?? Theme.of(context).colorScheme.background, 261 | copyButtonText: copyButtonText ?? dartCodeViewerThemeData.copyButtonText ?? _defaultCopyButtonText, 262 | showCopyButton: showCopyButton ?? dartCodeViewerThemeData.showCopyButton ?? _defaultShowCopyButton, 263 | height: height ?? dartCodeViewerThemeData.height ?? MediaQuery.of(context).size.height, 264 | width: width ?? dartCodeViewerThemeData.width ?? MediaQuery.of(context).size.width, 265 | ); 266 | 267 | return DartCodeViewerTheme( 268 | data: dartCodeViewerThemeData, 269 | child: Container( 270 | color: dartCodeViewerThemeData.backgroundColor, 271 | padding: const EdgeInsets.symmetric(horizontal: 16), 272 | height: dartCodeViewerThemeData.height, 273 | width: dartCodeViewerThemeData.width, 274 | child: _DartCodeViewerPage( 275 | codifyString(data, dartCodeViewerThemeData), 276 | ), 277 | ), 278 | ); 279 | } 280 | 281 | InlineSpan codifyString(String content, DartCodeViewerThemeData dartCodeViewerThemeData) { 282 | List textSpans = []; 283 | final codeSpans = DartSyntaxPrehighlighter().format(content); 284 | // Converting CodeSpan to TextSpan by first converting to String and then TextSpan. 285 | for (final span in codeSpans) { 286 | textSpans.add(stringToTextSpan(span.toString(), dartCodeViewerThemeData)); 287 | } 288 | return TextSpan(children: textSpans); 289 | } 290 | 291 | TextSpan stringToTextSpan(String string, DartCodeViewerThemeData dartCodeViewerThemeData) { 292 | return TextSpan( 293 | style: () { 294 | String styleString = RegExp(r'codeStyle.\w*').firstMatch(string).group(0); 295 | DartCodeViewerThemeData dartCodeViewerTheme = dartCodeViewerThemeData; 296 | 297 | switch (styleString) { 298 | case 'codeStyle.baseStyle': 299 | return dartCodeViewerTheme.baseStyle; 300 | case 'codeStyle.numberStyle': 301 | return dartCodeViewerTheme.numberStyle; 302 | case 'codeStyle.commentStyle': 303 | return dartCodeViewerTheme.commentStyle; 304 | case 'codeStyle.keywordStyle': 305 | return dartCodeViewerTheme.keywordStyle; 306 | case 'codeStyle.stringStyle': 307 | return dartCodeViewerTheme.stringStyle; 308 | case 'codeStyle.punctuationStyle': 309 | return dartCodeViewerTheme.punctuationStyle; 310 | case 'codeStyle.classStyle': 311 | return dartCodeViewerTheme.classStyle; 312 | case 'codeStyle.constantStyle': 313 | return dartCodeViewerTheme.constantStyle; 314 | default: 315 | return dartCodeViewerTheme.baseStyle; 316 | } 317 | }(), 318 | text: () { 319 | String textString = RegExp('\'.*\'').firstMatch(string).group(0); 320 | String subString = textString.substring(1, textString.length - 1); 321 | return decodeString(subString); 322 | }(), 323 | ); 324 | } 325 | 326 | /// Read raw string as regular String. Converts Unicode characters to actual 327 | /// numbers. 328 | String decodeString(String string) { 329 | return string 330 | .replaceAll(r'\u000a', '\n') 331 | .replaceAll(r'\u0027', '\'') 332 | .replaceAll(r'\u009', '\t'); 333 | } 334 | } 335 | 336 | class _DartCodeViewerPage extends StatelessWidget { 337 | const _DartCodeViewerPage(this.code); 338 | final InlineSpan code; 339 | 340 | @override 341 | Widget build(BuildContext context) { 342 | final _richTextCode = code; 343 | final _plainTextCode = _richTextCode.toPlainText(); 344 | 345 | void _showSnackBarOnCopySuccess(dynamic result) { 346 | Scaffold.of(context).showSnackBar( 347 | SnackBar( 348 | content: Text('Copied to Clipboard'), 349 | ), 350 | ); 351 | } 352 | 353 | void _showSnackBarOnCopyFailure(Object exception) { 354 | Scaffold.of(context).showSnackBar( 355 | SnackBar( 356 | content: Text('Failure to copy to clipboard: $exception'), 357 | ), 358 | ); 359 | } 360 | 361 | return Column( 362 | crossAxisAlignment: CrossAxisAlignment.start, 363 | children: [ 364 | if (DartCodeViewerTheme.of(context).showCopyButton) 365 | FlatButton( 366 | color: Colors.white.withOpacity(0.15), 367 | materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, 368 | padding: const EdgeInsets.symmetric(horizontal: 8), 369 | shape: const RoundedRectangleBorder( 370 | borderRadius: BorderRadius.all(Radius.circular(4)), 371 | ), 372 | onPressed: () async { 373 | await Clipboard.setData( 374 | ClipboardData(text: _plainTextCode) 375 | ).then(_showSnackBarOnCopySuccess) 376 | .catchError(_showSnackBarOnCopyFailure); 377 | }, 378 | child: DartCodeViewerTheme.of(context).copyButtonText, 379 | ), 380 | Expanded( 381 | child: SingleChildScrollView( 382 | child: RichText( 383 | textDirection: TextDirection.ltr, 384 | text: _richTextCode, 385 | ), 386 | ), 387 | ), 388 | ], 389 | ); 390 | } 391 | } 392 | 393 | '''; 394 | testWidgets('Dart Code Viewer Default', (WidgetTester tester) async { 395 | await tester.pumpWidget( 396 | MaterialApp( 397 | home: Scaffold(body: DartCodeViewer(data)), 398 | ), 399 | ); 400 | }); 401 | } 402 | -------------------------------------------------------------------------------- /test/dart_code_viewer_theme_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | import 'package:flutter_test/flutter_test.dart'; 3 | import 'package:dart_code_viewer/dart_code_viewer.dart'; 4 | 5 | void main() { 6 | String data = r''' 7 | import 'package:flutter/material.dart'; 8 | import 'package:flutter/services.dart'; 9 | import 'package:google_fonts/google_fonts.dart'; 10 | import 'dart_code_viewer_theme.dart'; 11 | import 'pre_highlighter.dart'; 12 | 13 | class DartCodeViewer extends StatelessWidget { 14 | const DartCodeViewer( 15 | this.data, { 16 | Key key, 17 | this.baseStyle, 18 | this.classStyle, 19 | this.commentStyle, 20 | this.constantStyle, 21 | this.keywordStyle, 22 | this.numberStyle, 23 | this.punctuationStyle, 24 | this.stringStyle, 25 | this.backgroundColor, 26 | this.copyButtonText, 27 | this.showCopyButton, 28 | this.height, 29 | this.width, 30 | }) : assert(data != null, 'A non-null String must be provided to a DartCodeViewer widget.'), 31 | super(key: key); 32 | 33 | factory DartCodeViewer.textColor( 34 | String data, { 35 | TextStyle textStyle, 36 | Color baseColor, 37 | Color classColor, 38 | Color commentColor, 39 | Color constantColor, 40 | Color keywordColor, 41 | Color numberColor, 42 | Color punctuationColor, 43 | Color stringColor, 44 | Color backgroundColor, 45 | Text copyButtonText, 46 | bool showCopyButton, 47 | double height, 48 | double width, 49 | }) { 50 | TextStyle _defaultCodeStyle = textStyle ?? GoogleFonts.robotoMono(fontSize: 12); 51 | return DartCodeViewer( 52 | data, 53 | baseStyle: _defaultCodeStyle.copyWith(color: baseColor), 54 | classStyle: _defaultCodeStyle.copyWith(color: classColor), 55 | commentStyle: _defaultCodeStyle.copyWith(color: commentColor), 56 | constantStyle: _defaultCodeStyle.copyWith(color: constantColor), 57 | keywordStyle: _defaultCodeStyle.copyWith(color: keywordColor), 58 | numberStyle: _defaultCodeStyle.copyWith(color: numberColor), 59 | punctuationStyle: _defaultCodeStyle.copyWith(color: punctuationColor), 60 | stringStyle: _defaultCodeStyle.copyWith(color: stringColor), 61 | backgroundColor: backgroundColor, 62 | copyButtonText: copyButtonText, 63 | showCopyButton: showCopyButton, 64 | height: height, 65 | width: width, 66 | ); 67 | } 68 | 69 | factory DartCodeViewer.light(String data) { 70 | return DartCodeViewer.textColor( 71 | data, 72 | baseColor: Colors.blueGrey.shade800, 73 | classColor: Colors.purple.shade500, 74 | commentColor: Colors.pink.shade600, 75 | constantColor: Colors.indigo.shade50, 76 | keywordColor: Colors.indigo.shade500, 77 | numberColor: Colors.red.shade700, 78 | punctuationColor: Colors.blueGrey.shade800, 79 | stringColor: Colors.green.shade700, 80 | backgroundColor: Colors.grey.shade100, 81 | ); 82 | } 83 | 84 | factory DartCodeViewer.lightAlt(String data) { 85 | return DartCodeViewer.textColor( 86 | data, 87 | baseColor: Colors.black, 88 | classColor: Color(0xFF673AB7), 89 | commentColor: Color(0xFF999999), 90 | constantColor: Color(0xFFE67C73), 91 | keywordColor: Color(0xFF4285F4), 92 | numberColor: Color(0xFFDB4437), 93 | punctuationColor: Color(0xFFA3A3A3), 94 | stringColor: Color(0xFF0F9D58), 95 | backgroundColor: Color(0xFFEEEEEE), 96 | ); 97 | } 98 | 99 | factory DartCodeViewer.dark(String data) { 100 | return DartCodeViewer.textColor( 101 | data, 102 | baseColor: Colors.blueGrey.shade50, 103 | classColor: Colors.purple.shade200, 104 | commentColor: Colors.pink.shade300, 105 | constantColor: Colors.yellow.shade700, 106 | keywordColor: Colors.cyan.shade300, 107 | numberColor: Colors.yellow.shade700, 108 | punctuationColor: Colors.blueGrey.shade50, 109 | stringColor: Colors.lightGreen.shade400, 110 | backgroundColor: Colors.grey.shade900, 111 | ); 112 | } 113 | 114 | factory DartCodeViewer.darkAlt(String data) { 115 | return DartCodeViewer.textColor( 116 | data, 117 | baseColor: Colors.white, 118 | classColor: Color(0xFFFF8A65), 119 | commentColor: Color(0xFFAAAAAA), 120 | constantColor: Color(0xFFE67C73), 121 | keywordColor: Color(0xFF7BAAF7), 122 | numberColor: Color(0xFFF4B400), 123 | punctuationColor: Color(0xFFA3A3A3), 124 | stringColor: Color(0xFF57BB8A), 125 | backgroundColor: Color(0xFF000000), 126 | ); 127 | } 128 | 129 | factory DartCodeViewer.designDark(String data) { 130 | return DartCodeViewer.textColor( 131 | data, 132 | baseColor: Colors.white, 133 | classColor: Color(0xFFFF8A80), 134 | commentColor: Color(0xFF607D8B), 135 | constantColor: Color(0xFF90A4AE), 136 | keywordColor: Color(0xFF26C6DA), 137 | numberColor: Color(0xFFFFBC00), 138 | punctuationColor: Color(0xFF90A4AE), 139 | stringColor: Color(0xFF00BFA4), 140 | backgroundColor: Color(0xFF263238), 141 | ); 142 | } 143 | 144 | factory DartCodeViewer.io17(String data) { 145 | return DartCodeViewer.textColor( 146 | data, 147 | baseColor: Colors.white, 148 | classColor: Color(0xFFFF8857), 149 | commentColor: Color(0xFFFF5CB4), 150 | constantColor: Color(0xFF90A4AE), 151 | keywordColor: Color(0xFF00E4FF), 152 | numberColor: Color(0xFFFFD500), 153 | punctuationColor: Color(0xFF90A4AE), 154 | stringColor: Color(0xFF1CE8b5), 155 | backgroundColor: Color(0xFF263238), 156 | ); 157 | } 158 | 159 | factory DartCodeViewer.io19(String data) { 160 | return DartCodeViewer.textColor( 161 | data, 162 | baseColor: Colors.white, 163 | classColor: Color(0xFFEE675C), 164 | commentColor: Color(0xFF9AA0A6), 165 | constantColor: Color(0xFFFCC934), 166 | keywordColor: Color(0xFF669DF6), 167 | numberColor: Color(0xFFFCC934), 168 | punctuationColor: Color(0xFF9AA0A6), 169 | stringColor: Color(0xFF5BB974), 170 | backgroundColor: Color(0xFF202124), 171 | ); 172 | } 173 | 174 | factory DartCodeViewer.flutterInteract19(String data) { 175 | return DartCodeViewer.textColor( 176 | data, 177 | baseColor: Color(0xFFFAFBFB), 178 | classColor: Color(0xFFD65BAD), 179 | commentColor: Color(0xFF808080), 180 | constantColor: Color(0xFFFF8383), 181 | keywordColor: Color(0xFF1CDEC9), 182 | numberColor: Color(0xFFBD93F9), 183 | punctuationColor: Color(0xFF8BE9FD), 184 | stringColor: Color(0xFFffa65c), 185 | backgroundColor: Color(0xFF241e30), 186 | ); 187 | } 188 | 189 | final TextStyle baseStyle; 190 | final TextStyle classStyle; 191 | final TextStyle commentStyle; 192 | final TextStyle constantStyle; 193 | final TextStyle keywordStyle; 194 | final TextStyle numberStyle; 195 | final TextStyle punctuationStyle; 196 | final TextStyle stringStyle; 197 | final String data; 198 | 199 | final Color backgroundColor; 200 | final Text copyButtonText; 201 | final bool showCopyButton; 202 | 203 | final double height; 204 | final double width; 205 | 206 | @override 207 | Widget build(BuildContext context) { 208 | final codeTextStyle = GoogleFonts.robotoMono( 209 | fontSize: 12 * MediaQuery.of(context).textScaleFactor, 210 | ); 211 | 212 | /// Uses device brightness to choose what type of theme to return. 213 | final brightness = WidgetsBinding.instance.window.platformBrightness; 214 | bool lightModeOn = brightness == Brightness.light; 215 | 216 | TextStyle _defaultBaseStyle = codeTextStyle.copyWith( 217 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50 218 | ); 219 | TextStyle _defaultClassStyle = codeTextStyle.copyWith( 220 | color: lightModeOn ? Colors.purple.shade500 : Colors.purple.shade200 221 | ); 222 | TextStyle _defaultCommentStyle = codeTextStyle.copyWith( 223 | color: lightModeOn ? Colors.pink.shade600 : Colors.pink.shade300 224 | ); 225 | TextStyle _defaultConstantStyle = codeTextStyle.copyWith( 226 | color: lightModeOn ? Colors.indigo.shade50 : Colors.yellow.shade700 227 | ); 228 | TextStyle _defaultKeywordStyle = codeTextStyle.copyWith( 229 | color: lightModeOn ? Colors.indigo.shade500 : Colors.cyan.shade300 230 | ); 231 | TextStyle _defaultNumberStyle = codeTextStyle.copyWith( 232 | color: lightModeOn ? Colors.red.shade700 : Colors.yellow.shade700 233 | ); 234 | TextStyle _defaultPunctuationalStyle = codeTextStyle.copyWith( 235 | color: lightModeOn ? Colors.blueGrey.shade800 : Colors.blueGrey.shade50 236 | ); 237 | TextStyle _defaultStringStyle = codeTextStyle.copyWith( 238 | color: lightModeOn ? Colors.green.shade700 : Colors.lightGreen.shade400 239 | ); 240 | 241 | Text _defaultCopyButtonText = Text('' 242 | 'COPY ALL', 243 | style: Theme.of(context).textTheme.button.copyWith( 244 | color: Colors.white, 245 | fontWeight: FontWeight.w500, 246 | ), 247 | ); 248 | bool _defaultShowCopyButton = true; 249 | 250 | DartCodeViewerThemeData dartCodeViewerThemeData = DartCodeViewerTheme.of(context); 251 | dartCodeViewerThemeData = dartCodeViewerThemeData.copyWith( 252 | baseStyle: baseStyle ?? dartCodeViewerThemeData.baseStyle ?? _defaultBaseStyle, 253 | classStyle: classStyle ?? dartCodeViewerThemeData.classStyle ?? _defaultClassStyle, 254 | commentStyle: commentStyle ?? dartCodeViewerThemeData.commentStyle ?? _defaultCommentStyle, 255 | constantStyle: constantStyle ?? dartCodeViewerThemeData.constantStyle ?? _defaultConstantStyle, 256 | keywordStyle: keywordStyle ?? dartCodeViewerThemeData.keywordStyle ?? _defaultKeywordStyle, 257 | numberStyle: numberStyle ?? dartCodeViewerThemeData.numberStyle ?? _defaultNumberStyle, 258 | punctuationStyle: punctuationStyle ?? dartCodeViewerThemeData.punctuationStyle ?? _defaultPunctuationalStyle, 259 | stringStyle: stringStyle ?? dartCodeViewerThemeData.stringStyle ?? _defaultStringStyle, 260 | backgroundColor: backgroundColor ?? dartCodeViewerThemeData.backgroundColor ?? Theme.of(context).colorScheme.background, 261 | copyButtonText: copyButtonText ?? dartCodeViewerThemeData.copyButtonText ?? _defaultCopyButtonText, 262 | showCopyButton: showCopyButton ?? dartCodeViewerThemeData.showCopyButton ?? _defaultShowCopyButton, 263 | height: height ?? dartCodeViewerThemeData.height ?? MediaQuery.of(context).size.height, 264 | width: width ?? dartCodeViewerThemeData.width ?? MediaQuery.of(context).size.width, 265 | ); 266 | 267 | return DartCodeViewerTheme( 268 | data: dartCodeViewerThemeData, 269 | child: Container( 270 | color: dartCodeViewerThemeData.backgroundColor, 271 | padding: const EdgeInsets.symmetric(horizontal: 16), 272 | height: dartCodeViewerThemeData.height, 273 | width: dartCodeViewerThemeData.width, 274 | child: _DartCodeViewerPage( 275 | codifyString(data, dartCodeViewerThemeData), 276 | ), 277 | ), 278 | ); 279 | } 280 | 281 | InlineSpan codifyString(String content, DartCodeViewerThemeData dartCodeViewerThemeData) { 282 | List textSpans = []; 283 | final codeSpans = DartSyntaxPrehighlighter().format(content); 284 | // Converting CodeSpan to TextSpan by first converting to String and then TextSpan. 285 | for (final span in codeSpans) { 286 | textSpans.add(stringToTextSpan(span.toString(), dartCodeViewerThemeData)); 287 | } 288 | return TextSpan(children: textSpans); 289 | } 290 | 291 | TextSpan stringToTextSpan(String string, DartCodeViewerThemeData dartCodeViewerThemeData) { 292 | return TextSpan( 293 | style: () { 294 | String styleString = RegExp(r'codeStyle.\w*').firstMatch(string).group(0); 295 | DartCodeViewerThemeData dartCodeViewerTheme = dartCodeViewerThemeData; 296 | 297 | switch (styleString) { 298 | case 'codeStyle.baseStyle': 299 | return dartCodeViewerTheme.baseStyle; 300 | case 'codeStyle.numberStyle': 301 | return dartCodeViewerTheme.numberStyle; 302 | case 'codeStyle.commentStyle': 303 | return dartCodeViewerTheme.commentStyle; 304 | case 'codeStyle.keywordStyle': 305 | return dartCodeViewerTheme.keywordStyle; 306 | case 'codeStyle.stringStyle': 307 | return dartCodeViewerTheme.stringStyle; 308 | case 'codeStyle.punctuationStyle': 309 | return dartCodeViewerTheme.punctuationStyle; 310 | case 'codeStyle.classStyle': 311 | return dartCodeViewerTheme.classStyle; 312 | case 'codeStyle.constantStyle': 313 | return dartCodeViewerTheme.constantStyle; 314 | default: 315 | return dartCodeViewerTheme.baseStyle; 316 | } 317 | }(), 318 | text: () { 319 | String textString = RegExp('\'.*\'').firstMatch(string).group(0); 320 | String subString = textString.substring(1, textString.length - 1); 321 | return decodeString(subString); 322 | }(), 323 | ); 324 | } 325 | 326 | /// Read raw string as regular String. Converts Unicode characters to actual 327 | /// numbers. 328 | String decodeString(String string) { 329 | return string 330 | .replaceAll(r'\u000a', '\n') 331 | .replaceAll(r'\u0027', '\'') 332 | .replaceAll(r'\u009', '\t'); 333 | } 334 | } 335 | 336 | class _DartCodeViewerPage extends StatelessWidget { 337 | const _DartCodeViewerPage(this.code); 338 | final InlineSpan code; 339 | 340 | @override 341 | Widget build(BuildContext context) { 342 | final _richTextCode = code; 343 | final _plainTextCode = _richTextCode.toPlainText(); 344 | 345 | void _showSnackBarOnCopySuccess(dynamic result) { 346 | Scaffold.of(context).showSnackBar( 347 | SnackBar( 348 | content: Text('Copied to Clipboard'), 349 | ), 350 | ); 351 | } 352 | 353 | void _showSnackBarOnCopyFailure(Object exception) { 354 | Scaffold.of(context).showSnackBar( 355 | SnackBar( 356 | content: Text('Failure to copy to clipboard: $exception'), 357 | ), 358 | ); 359 | } 360 | 361 | return Column( 362 | crossAxisAlignment: CrossAxisAlignment.start, 363 | children: [ 364 | if (DartCodeViewerTheme.of(context).showCopyButton) 365 | FlatButton( 366 | color: Colors.white.withOpacity(0.15), 367 | materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, 368 | padding: const EdgeInsets.symmetric(horizontal: 8), 369 | shape: const RoundedRectangleBorder( 370 | borderRadius: BorderRadius.all(Radius.circular(4)), 371 | ), 372 | onPressed: () async { 373 | await Clipboard.setData( 374 | ClipboardData(text: _plainTextCode) 375 | ).then(_showSnackBarOnCopySuccess) 376 | .catchError(_showSnackBarOnCopyFailure); 377 | }, 378 | child: DartCodeViewerTheme.of(context).copyButtonText, 379 | ), 380 | Expanded( 381 | child: SingleChildScrollView( 382 | child: RichText( 383 | textDirection: TextDirection.ltr, 384 | text: _richTextCode, 385 | ), 386 | ), 387 | ), 388 | ], 389 | ); 390 | } 391 | } 392 | 393 | '''; 394 | testWidgets('Dart Code Viewer Default', (WidgetTester tester) async { 395 | await tester.pumpWidget( 396 | MaterialApp( 397 | home: Scaffold( 398 | body: DartCodeViewerTheme( 399 | data: DartCodeViewerThemeData( 400 | width: 100, 401 | ), 402 | child: DartCodeViewer(data), 403 | ), 404 | ), 405 | ), 406 | ); 407 | }); 408 | } 409 | --------------------------------------------------------------------------------