An open API service indexing awesome lists of open source software.

https://github.com/viztushar/syntax_highlighter

Syntax Highlighter for Dart/Flutter Code
https://github.com/viztushar/syntax_highlighter

dart dart-syntax-highlighter flutter flutter-apps flutter-demo flutter-package flutter-syntax flutter-syntax-highlighter syntax syntax-highlighter syntax-highlighting

Last synced: 5 months ago
JSON representation

Syntax Highlighter for Dart/Flutter Code

Awesome Lists containing this project

README

          

# syntax_highlighter

syntax highlighter for show your code in the app

# How to Use It

### Step 2

add your code in your file

```javascript
String _exampleCode =
"class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState();}";
```

### Step 2

add `SyntaxHighlighterStyle` in your `build` method

```javascript
final SyntaxHighlighterStyle style =
Theme.of(context).brightness == Brightness.dark
? SyntaxHighlighterStyle.darkThemeStyle()
: SyntaxHighlighterStyle.lightThemeStyle();
```

### Step 3

add this code

```javascript
RichText(
text: TextSpan(
style: const TextStyle(fontFamily: 'monospace', fontSize: 10.0),
children: [
DartSyntaxHighlighter(style).format(_exampleCode),
],
),
```

# Full Example

check Full Example code in [example/lib/main.dart](https://github.com/viztushar/syntax_highlighter/blob/master/example/lib/main.dart)

```javascript
import 'package:flutter/material.dart';
import 'package:syntax_highlighter/syntax_highlighter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Syntax Highlighter Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Syntax Highlighter Example'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
String _exampleCode =
"class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState();}";

@override
Widget build(BuildContext context) {
final SyntaxHighlighterStyle style =
Theme.of(context).brightness == Brightness.dark
? SyntaxHighlighterStyle.darkThemeStyle()
: SyntaxHighlighterStyle.lightThemeStyle();
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: RichText(
text: TextSpan(
style: const TextStyle(fontFamily: 'monospace', fontSize: 10.0),
children: [
DartSyntaxHighlighter(style).format(_exampleCode),
],
),
),
),
));
}
}

```