Skip to content

Commit

Permalink
added code view
Browse files Browse the repository at this point in the history
  • Loading branch information
yc-codes committed Mar 3, 2020
1 parent 12c40f9 commit 9471ef8
Show file tree
Hide file tree
Showing 29 changed files with 1,002 additions and 478 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
# dart_star_pattern
# Star Patterns in Dart

A new Flutter project.
My senior given this task.

## Getting Started
## Resources

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
- [Widget with codeview by X-Wei](https://github.com/X-Wei/widget_with_codeview) - I modified some code.
66 changes: 66 additions & 0 deletions lib/codeviewer/source_code_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'syntax_highlighter.dart';

class SourceCodeView extends StatefulWidget {
final String filePath;

const SourceCodeView({
Key key,
@required this.filePath,
}) : super(key: key);

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

class _SourceCodeViewState extends State<SourceCodeView> {
double _textScaleFactor = 1.0;
Widget _getCodeView(String codeContent, BuildContext context) {
final SyntaxHighlighterStyle style =
Theme.of(context).brightness == Brightness.dark
? SyntaxHighlighterStyle.darkThemeStyle()
: SyntaxHighlighterStyle.lightThemeStyle();
return Container(
constraints: BoxConstraints.expand(),
child: SingleChildScrollView(
padding: EdgeInsets.only(left: 6, top: 8),
child: SelectableText.rich(
TextSpan(
style: TextStyle(fontFamily: 'monospace', fontSize: 12.0)
.apply(fontSizeFactor: this._textScaleFactor),
children: <TextSpan>[
DartSyntaxHighlighter(style).format(codeContent)
],
),
style: DefaultTextStyle.of(context)
.style
.apply(fontSizeFactor: this._textScaleFactor),
),
),
);
}

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: DefaultAssetBundle.of(context).loadString(widget.filePath) ??
'Error loading source code from $this.filePath',
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasData) {
return Scaffold(
body: Padding(
padding: EdgeInsets.all(4.0),
child: _getCodeView(snapshot.data, context),
),
);
} else {
return Center(child: CircularProgressIndicator());
}
},
);
}
}
Loading

0 comments on commit 9471ef8

Please sign in to comment.