Git Product home page Git Product logo

viztushar / syntax_highlighter Goto Github PK

View Code? Open in Web Editor NEW
31.0 4.0 9.0 69 KB

Syntax Highlighter for Dart/Flutter Code

Home Page: https://pub.dev/packages/syntax_highlighter

License: Apache License 2.0

Java 2.45% Objective-C 5.07% Dart 88.68% Shell 3.81%
flutter flutter-package flutter-demo flutter-apps dart syntax-highlighting syntax flutter-syntax syntax-highlighter flutter-syntax-highlighter dart-syntax-highlighter

syntax_highlighter's Introduction

syntax_highlighter

syntax highlighter for show your code in the app

How to Use It

Step 2

add your code in your file

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

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

Step 3

add this code

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

Full Example

check Full Example code in example/lib/main.dart

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<MyHomePage> {
  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: <TextSpan>[
                  DartSyntaxHighlighter(style).format(_exampleCode),
                ],
              ),
            ),
          ),
        ));
  }
}

syntax_highlighter's People

Contributors

victoreronmosele avatar viztushar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

syntax_highlighter's Issues

Syntax highlighter doesn't expand beyond 5 lines - Web

I am trying to come up with a code editor which takes input from a text form field and shows the output on a Rich Text widget using DartSyntaxHighlighter.

While it works fine for a short snippet of code, it doesn't show all the code for a larger snippet. Here's what I've done so far :

class CodeEditorWidget extends StatefulWidget {

  CodeEditorWidget();

  @override
  _ContentWidgetState createState() {
    return _ContentWidgetState();
  }
}

class _ContentWidgetState extends BaseState<CodeEditorWidget> {

  String _currentCode = "";

  @override
  void initializeData() {
    _currentCode = "class HelloWorld {\n"
        "public static void main() {\n"
        "System.out.println(\"Hello again\");\n"
        "}\n"
        "}";
    _contentController.addListener(() {
      _currentCode = _contentController.value.text;
      setState(() {

      });
    });
  }

  @override
  Widget build(BuildContext context) => _buildContent();

  Widget _buildContent() {
    //return _buildBody();
    userState = AppStateWidget.of(context).userState;
    return _buildBody();
  }

  Scaffold _buildBody() => Scaffold(
    key: _scaffoldLoginKey,
    appBar: buildAppBar("Code Editor"),
    body: _buildCodeEditor(),
    floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    floatingActionButton: _buildFab(),
  );

  _buildCodeEditor() => Card(
    margin: const EdgeInsets.fromLTRB(BaseState.horizontalMargin, 0, BaseState.horizontalMargin, 0),
    child: Column(
      children: <Widget>[
        Expanded(
          child: _buildCodeView()//buildSyntaxCodeBlock(_currentCode, 12)//_buildCodeView(),
        ),
        _buildInputContainer()
      ],
    ),
  );

  _buildCodeView() => SingleChildScrollView(
    scrollDirection: Axis.vertical,
    child: Padding(
      padding: const EdgeInsets.all(16.0),
      child: Container(
        width: double.infinity,
        height: double.maxFinite,
        padding: EdgeInsets.all(12),
        decoration: BoxDecoration(color: Colors.black),
        child: RichText(
          text: TextSpan(
            style: TextStyle(fontFamily: 'VarelaRound-Regular', fontSize: 12),
            children: <TextSpan>[
              DartSyntaxHighlighter(SyntaxHighlighterStyle.darkThemeStyle()).format(_currentCode)
            ],
          ),
        ),
      ),
    ),
  );

  Container _buildInputContainer() {
    return Container(
      color: Colors.grey,
      padding: EdgeInsets.all(8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _buildInputLayout(),
        ],
      ),
    );
  }

  _buildInputLayout() => Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: <Widget>[_buildTextForm()],
  );

  Widget _buildTextForm() => Flexible(
    child: Container(
      color: Colors.white,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: TextFormField(
          style: buildTextStyleBlack(16),
          decoration: InputDecoration.collapsed(hintText: "Type here..."),
          maxLines: 10,
          validator: _validateEmptyCode,
          controller: _contentController,
          keyboardType: TextInputType.multiline,
          onSaved: (String contentString) {
            //_currentCode = contentString;
          },
        ),
      ),
    ),
  );

  String _validateEmptyCode(String value) {
    return value.isEmpty ? "Required" : null;
  }

  var _contentController = TextEditingController();

  _buildFab() => FloatingActionButton(
    onPressed: () {
      setState(() {

      });
    },
    child: Icon(Icons.add),
    foregroundColor: Colors.white,
    backgroundColor: Colors.green,
  );

  var _scaffoldLoginKey = GlobalKey<ScaffoldState>();

  _showSnackBar(String message) => _scaffoldLoginKey.currentState
      .showSnackBar(SnackBar(content: Text(message, style: buildTextStyle(16),)));

}

Here's a screen shot for reference ::

https://i.stack.imgur.com/Zh9vS.png

Documentation

Hi, is this fully working yet? Can you provide a readme?

Would This Package Support Null Safety?

When I tried to use this package in my flutter app, the compiler told me that this package is out of date since it does not support Null Safety. dart(import_of_legacy_library_into_null_safe)

I haven't found any other option for highlighting my language.

@viztushar have you planned to update this package later?

Thank you for your patience.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.