Giter VIP home page Giter VIP logo

lucas-goldner / dart-code-viewer-x Goto Github PK

View Code? Open in Web Editor NEW

This project forked from verkit/dart_code_viewer2

0.0 1.0 0.0 7.73 MB

Fork of the original dart_code_viewer_2, will keep it updated in case I need it

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

License: The Unlicense

C++ 14.18% C 0.62% Objective-C 0.03% Kotlin 0.12% Dart 74.42% Swift 0.34% HTML 3.34% CMake 6.96%

dart-code-viewer-x's Introduction

Dart Code Viewer X

Get it from here: https://pub.dev/packages/dart_code_viewer_x

Disclaimer

This package is an updated safety version of dart_code_viewer2, which is a package, that is the null safety version of dart_code_viewer. I will try my best to keep this updated! Hopefully nobody needs to create a new fork of this package...

Update: The original author reached out and updated the package. I will keep this fork, though in case we need a quick update.
Update to the Update: We need to do some custom stuff especially for the implementation used in fluttershow_base so keep that in mind.

Join the discord for discussions about this package:

dart_code_viewer_x

The dart_code_viewer_x package for Flutter allows you to easily show and copy dart code in your Flutter application.

Additional features in this package:

  • Decide wether to toggle notifications for copying code
  • Provide your own copy button instead of using the default ElevatedButton

Getting Started

Dart Code Viewer Example in light and dark Theme Mode.

With the dart_code_viewer_x package, you can show Dart code in a Flutter application. The dart_code_viewer is documented well and uses Flutters pattern with Theming.

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.

The code viewer requires a non-null data String as required input.

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.

To use dart_code_viewer_x first you must add the 'dart_code_viewer_x' package to your oubspec dependencies

To import DartCodeViewer;

import 'package:dart_code_viewer_x/dart_code_viewer_x.dart';

To use DartCodeViewer with the default DartCodeViewer:

DartCodeViewer(r'''
void main() {
  runApp(MyApp());
}
'''
),

''' 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.

By simply using the code above you will get the example shown in the GIF.

You can also customize the theming of the DartCodeViewer by using its parameters:

DartCodeViewer(r'''
void main() {
  runApp(MyApp());
}
''',
  backgroundColor: GoogleFonts.robotoMono().copyWith(color: Colors.pink),
),

This will change the background color of the CodeViewer to Pink.

You can use this Code viewer tool 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.

Code Viewer Tool Dart Code Viewer Parameter
background backgroundColor
plain text baseStyle
Punctuation punctuationStyle
String, values stringStyle
Keywords, tags keywordStyle
Comments commentStyle
Types classStyle
Numbers numberStyle

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:

DartCodeViewer.textColor(r'''
void main() {
  runApp(MyApp());
}
''',
  textStyle: GoogleFonts.lato(),
  commentColor: Colors.grey,
  baseColor: Colors.pink,
),

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.

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:

  • DartCodeViewer.light
  • DartCodeViewer.lightAlt
  • DartCodeViewer.dark
  • DartCodeViewer.darkAlt
  • DartCodeViewer.designDark
  • DartCodeViewer.io17
  • DartCodeViewer.io19
  • DartCodeViewer.flutterInteract2019

Here is an example on how to use these themed code viewers.

DartCodeViewer.designDark(r'''
void main() {
  runApp(MyApp());
}
''',
),

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.

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.

Using the DartCodeViewerThemeData returns the data from the closest DartCodeViewerTheme instances the encloses the given context. The default parameters is set within the DartCodeViewer.

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].

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].

Here is an example below:

DartCodeViewerTheme(
  data: DartCodeViewerThemeData(
    backgroundColor: Colors.pink,
    copyButtonText: Text('Copiar'),
  ),
  child: DartCodeViewer.textColor(r'''
void main() {
  runApp(MyApp());
}
''',
  textStyle: GoogleFonts.lato(),
  commentColor: Colors.grey,
  baseColor: Colors.pink,
  ),
),

You can use this Code viewer tool 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.

Code Viewer Tool Dart Code Viewer Parameter
background backgroundColor
plain text baseStyle
Punctuation punctuationStyle
String, values stringStyle
Keywords, tags keywordStyle
Comments commentStyle
Types classStyle
Numbers numberStyle

dart-code-viewer-x's People

Contributors

justinekatebe69 avatar lucas-goldner avatar m97chahboun avatar verkit avatar

Watchers

 avatar

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.