Skip to content

Commit

Permalink
update: view remote code
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxu317317 committed Aug 15, 2019
1 parent cdb0697 commit 927a99e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 19 deletions.
31 changes: 23 additions & 8 deletions lib/components/full_screen_code_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/material.dart';

import 'package:flutter_go/utils/example_code_parser.dart';
import 'package:flutter_go/utils/syntax_highlighter.dart';
import 'package:flutter_go/utils/net_utils.dart';

class FullScreenCodeDialog extends StatefulWidget {
const FullScreenCodeDialog({this.filePath, this.remoteFilePath});
Expand All @@ -22,17 +23,31 @@ class _FullScreenCodeDialogState extends State<FullScreenCodeDialog> {
@override
void didChangeDependencies() {
print('widget.filePath=======${widget.filePath}');
getExampleCode(context,'${widget.filePath}', DefaultAssetBundle.of(context))
.then<void>((String code) {
if (mounted) {
setState(() {
_exampleCode = code ?? 'Example code not found';
});
}
});
if (widget.filePath != null) {
getExampleCode(context,'${widget.filePath}', DefaultAssetBundle.of(context))
.then<void>((String code) {
if (mounted) {
setState(() {
_exampleCode = code ?? 'Example code not found';
});
}
});
}
if (widget.remoteFilePath != null) {
getRemotePathCode(widget.remoteFilePath);
}

super.didChangeDependencies();
}

getRemotePathCode(path) async {
String response = await NetUtils.get(path);
if (mounted) {
setState(() {
_exampleCode = response ?? 'Example code not found';
});
}
}
@override
Widget build(BuildContext context) {
final SyntaxHighlighterStyle style =
Expand Down
2 changes: 1 addition & 1 deletion lib/components/widget_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _WidgetDemoState extends State<WidgetDemo> {
DataUtils.checkCollected(params).then((result) {
if (this.mounted) {
setState(() {
_hasCollected = result;
_hasCollected = result ?? null;
});
}
});
Expand Down
9 changes: 9 additions & 0 deletions lib/routers/router_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ var fullScreenCodeDialog = new Handler(
);
});


var githubCodeDialog = new Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
String path = params['remotePath']?.first;
return new FullScreenCodeDialog(
remoteFilePath: path,
);
});

var webViewPageHand = new Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
String title = params['title']?.first;
Expand Down
1 change: 1 addition & 0 deletions lib/routers/routers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Routes {
router.define('/category/error/404', handler: widgetNotFoundHandler);
router.define(loginPage, handler: loginPageHandler);
router.define(codeView,handler:fullScreenCodeDialog);
router.define(githubCodeView,handler:githubCodeDialog);
router.define(webViewPage,handler:webViewPageHand);
router.define(issuesMessage, handler: issuesMessageHandler);
widgetDemosList.forEach((demo) {
Expand Down
71 changes: 61 additions & 10 deletions lib/views/standard_demo_page/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import '../../components/flutter_markdown/lib/flutter_markdown.dart';
import '../../standard_pages/index.dart';
import '../../page_demo_package/index.dart';
import 'package:flutter_go/routers/application.dart';
import 'package:flutter_go/routers/routers.dart';
import 'package:flutter_go/utils/net_utils.dart';
import 'package:flutter_go/components/loading.dart';

const githubUrl = 'https://raw.githubusercontent.com/alibaba/flutter-go/beta/lib/standard_pages/';
const PagesUrl = 'https://raw.githubusercontent.com/alibaba/flutter-go/beta/lib/standard_pages/.pages.json';
const DemosUrl = 'https://raw.githubusercontent.com/alibaba/flutter-go/beta/lib/page_demo_package/.demo.json';
const githubHost = 'https://raw.githubusercontent.com/alibaba/flutter-go/beta';
const githubUrl = '$githubHost/lib/standard_pages/';
const PagesUrl = '$githubHost/lib/standard_pages/.pages.json';
const DemosUrl = '$githubHost/lib/page_demo_package/.demo.json';


// ONLINE || LOCAL
Expand Down Expand Up @@ -53,7 +55,7 @@ class _StandardView extends State<StandardView> {
Future<void> localGetPagesAttrsInfo() async {
String jsonString = await DefaultAssetBundle.of(context).loadString('lib/standard_pages/.pages.json');
List jsonList = json.decode(jsonString);
Map<String, dynamic> pageDetail = jsonList.firstWhere((item) => item['id'] == widget.id);
Map<String, dynamic> pageDetail = jsonList.firstWhere((item) => item['id'] == widget.id, orElse: null);

if (pageDetail != null) {
setState(() {
Expand All @@ -69,12 +71,10 @@ class _StandardView extends State<StandardView> {

String pageId = widget.id;
Map<String, String> pagesList = standardPage.getPages();
print('pagesList[pageId]>>> ${pagesList[pageId]}');
// print('pagesList[pageId]>>> ${pagesList[pageId]}');
return pagesList[pageId];
}
Future<String> getContentOnline() async {

String content = 'online content';
this.setState(() {
isLoading = true;
});
Expand Down Expand Up @@ -123,6 +123,24 @@ class _StandardView extends State<StandardView> {
}
return Future(() => conent);
}
void seeSourceCode(id) async {
List response;
try {
response = jsonDecode(await NetUtils.get(DemosUrl));
} catch (e) {
return alertDialog(msg: '请检查网络链接', title: '提示');
}

Map<String, dynamic> demoDetail = response.firstWhere((item) => item['id'] == id, orElse: null);
if (demoDetail == null) {
return null;
}

String remoteSouceCode = '$githubHost/lib/page_demo_package/${demoDetail['name']}_${demoDetail['author']}_${demoDetail['id']}/src/index.dart';
Application.router.navigateTo(context,
'${Routes.githubCodeView}?remotePath=${Uri.encodeComponent(remoteSouceCode)}');

}
Widget buildFootInfo() {
if (!isLoading) {
return Container(
Expand Down Expand Up @@ -160,12 +178,45 @@ class _StandardView extends State<StandardView> {
String errString = "not found ${attrs['id']} in demo packages";
debugPrint(errString);
demo = [Text(errString)];
return Column(children: demo);
} else {
return Column(
children: <Widget>[
Column(
children: demo,
),
Divider(
color: Theme.of(context).primaryColor,
),
InkWell(
onTap: () {
seeSourceCode(attrs['id']);
},
child: Text("查看源码", style: TextStyle(color: Theme.of(context).primaryColor)),
)
],
) ;
}

return Column(children: demo);
});
}

alertDialog({String msg, String title}) {
showDialog<Null>(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return new AlertDialog(
title: new Text(title),
content: new SingleChildScrollView(
child: new ListBody(
children: <Widget>[
new Text(msg),
],
),
)
);
},
);
}

@override
Widget build(BuildContext context) {
Expand Down

0 comments on commit 927a99e

Please sign in to comment.