Skip to content

Commit

Permalink
add code highlighter example
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Wei committed May 7, 2022
1 parent 5b52d53 commit ca0345d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/my_app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -915,7 +916,7 @@ const kMyAppRoutesAdvanced = <MyRouteGroup>[
),
MyRouteGroup(
groupName: 'Rich Text',
icon: Icon(Icons.history_edu),
icon: Icon(Icons.wysiwyg),
routes: <MyRoute>[
MyRoute(
child: MarkdownExample(),
Expand All @@ -929,6 +930,13 @@ const kMyAppRoutesAdvanced = <MyRouteGroup>[
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(
Expand Down
59 changes: 59 additions & 0 deletions lib/routes/richtext_code_highlight_ex.dart
Original file line number Diff line number Diff line change
@@ -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<CodeHighlightExample> createState() => _CodeHighlightExampleState();
}

const kLangToCodeSample = <String, String>{
'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<CodeHighlightExample> {
@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(),
],
),
],
);
}
}
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca0345d

Please sign in to comment.