A Flutter Package to Display Code in A Snippet View

Example

input :

import 'package:dart_snippet_view/dart_snippet_view.dart';
import 'package:flutter/material.dart';

class CodeViewer extends StatefulWidget {
  const CodeViewer({super.key});

  @override
  State<CodeViewer> createState() => _CodeViewerState();
}

class _CodeViewerState extends State<CodeViewer> {
  @override
  Widget build(BuildContext context) {
    return DartSnippetView(
      height: 500,
      width: 800,
      code: code(),
    );
  }

  String code() {
    return '''
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility in the flutter_test package. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:dart_style/dart_style.dart';
import 'package:example/fy_gen/sample.fsg.dart';
import 'package:example/home.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('code finder test', () {
    DartFormatter formatter = DartFormatter();
    String formattedCode =
        formatter.format(FSGCode.getCodeByType<Home>() ?? '');
    print(formattedCode);
  });
}
''';
  }
}


output :

Image

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add dart_snippet_view

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  dart_snippet_view: ^0.0.1

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:dart_snippet_view/dart_snippet_view.dart'; 

example/lib/main.dart

import 'package:example/code_viewer.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    const MaterialApp(
      home: CodeViewer(),
    ),
  );
} 

Download details:

Author: t0uh33d

Source: https://github.com/t0uh33d/dart_snippet_view

#flutter #dart #snippet 

A Flutter Package to Display Code in A Snippet View
1.35 GEEK