From ca0345d12494ea481c778ca6dd29277bd6c47187 Mon Sep 17 00:00:00 2001 From: x-wei Date: Sat, 7 May 2022 23:15:24 +0200 Subject: [PATCH] add code highlighter example --- lib/my_app_routes.dart | 10 +++- lib/routes/richtext_code_highlight_ex.dart | 59 ++++++++++++++++++++++ pubspec.lock | 14 +++++ pubspec.yaml | 1 + 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 lib/routes/richtext_code_highlight_ex.dart diff --git a/lib/my_app_routes.dart b/lib/my_app_routes.dart index 178c3b6..ada19cc 100644 --- a/lib/my_app_routes.dart +++ b/lib/my_app_routes.dart @@ -81,6 +81,7 @@ import 'routes/plugins_feature_discovery_ex.dart'; import 'routes/plugins_local_auth_ex.dart'; import 'routes/plugins_shimmer_ex.dart'; import 'routes/plugins_webview_ex.dart'; +import 'routes/richtext_code_highlight_ex.dart'; import 'routes/richtext_markdown_ex.dart'; import 'routes/richtext_quill_ex.dart'; import 'routes/state_bloc_ex.dart'; @@ -915,7 +916,7 @@ const kMyAppRoutesAdvanced = [ ), MyRouteGroup( groupName: 'Rich Text', - icon: Icon(Icons.history_edu), + icon: Icon(Icons.wysiwyg), routes: [ MyRoute( child: MarkdownExample(), @@ -929,6 +930,13 @@ const kMyAppRoutesAdvanced = [ description: 'Rich text editor', links: {'pub.dev': 'https://pub.dev/packages/flutter_quill'}, ), + MyRoute( + child: CodeHighlightExample(), + sourceFilePath: 'lib/routes/richtext_code_highlight_ex.dart', + title: 'Code highlight', + description: 'Syntax highlighting for different programming languages', + links: {'pub.dev': 'https://pub.dev/packages/flutter_highlight'}, + ), ], ), MyRouteGroup( diff --git a/lib/routes/richtext_code_highlight_ex.dart b/lib/routes/richtext_code_highlight_ex.dart new file mode 100644 index 0000000..b3f5a41 --- /dev/null +++ b/lib/routes/richtext_code_highlight_ex.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_highlight/flutter_highlight.dart'; +import 'package:flutter_highlight/themes/github.dart'; + +class CodeHighlightExample extends StatefulWidget { + const CodeHighlightExample({Key? key}) : super(key: key); + + @override + State createState() => _CodeHighlightExampleState(); +} + +const kLangToCodeSample = { + 'dart': ''' +main() { + print("Hello, World!"); +}''', + 'python': 'print("Hello World")', + 'cpp': ''' +#include +using namespace std; +int main () { + cout << "Hello World!"; + return 0; +}''', + 'java': ''' +class HelloWorldApp { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +}''', + 'SQL': "SELECT Country FROM Customers WHERE Country <> 'USA'", +}; + +class _CodeHighlightExampleState extends State { + @override + Widget build(BuildContext context) { + return ListView( + padding: const EdgeInsets.all(8), + children: [ + for (final entry in kLangToCodeSample.entries) + Column( + mainAxisSize: MainAxisSize.min, + children: [ + Text( + 'language=${entry.key}', + // strutStyle: Theme.of(context).textTheme.headline2, + ), + HighlightView( + entry.value, + language: entry.key, + theme: githubTheme, + ), + Divider(), + ], + ), + ], + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index b215743..571bafc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -545,6 +545,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.2" + flutter_highlight: + dependency: "direct main" + description: + name: flutter_highlight + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.0" flutter_inappwebview: dependency: transitive description: @@ -874,6 +881,13 @@ packages: url: "https://github.com/CouldI/flutter_heatmap_calendar.git" source: git version: "1.2.8" + highlight: + dependency: transitive + description: + name: highlight + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.0" hive: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index c6340fe..8635abe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: sdk: flutter flutter_bloc: ^8.0.1 flutter_gallery_assets: ^1.0.2 + flutter_highlight: ^0.7.0 flutter_markdown: ^0.6.9 flutter_quill: ^4.1.8 flutter_radar_chart: ^0.2.0