-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
558 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
# flutter_test_http | ||
|
||
A new Flutter application. | ||
Flutter application to check HTTP requests to API server http://json.flutter.su or any other. | ||
|
||
## Getting Started | ||
|
||
This project is a starting point for a Flutter application. | ||
You can: | ||
- git clone https://github.com/flutter-tutorial/test_http.git | ||
- download zip [](https://github.com/flutter-tutorial/test_http/archive/master.zip) | ||
|
||
A few resources to get you started if this is your first Flutter project: | ||
For help API, view our | ||
[Manual: TEST HTTP API](https://json.flutter.su/) | ||
[Flutter Lesson 7. HTTP and HTTPS network requests to Dart and Flutter](https://flutter.su/tutorial/7-HTTP-network-requests) | ||
|
||
- [Lab: Write your first Flutter app](https://flutter.io/docs/get-started/codelab) | ||
- [Cookbook: Useful Flutter samples](https://flutter.io/docs/cookbook) | ||
|
||
For help getting started with Flutter, view our | ||
[online documentation](https://flutter.io/docs), which offers tutorials, | ||
samples, guidance on mobile development, and a full API reference. | ||
![Screenshot](assets/screenshot_01.jpg) | ||
![Screenshot](assets/screenshot_02.jpg) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import './syntax_highlighter.dart'; | ||
|
||
class MyCodeView extends StatefulWidget { | ||
final String filePath; | ||
|
||
MyCodeView({@required this.filePath}); | ||
|
||
@override | ||
MyCodeViewState createState() { | ||
return MyCodeViewState(); | ||
} | ||
} | ||
|
||
class MyCodeViewState extends State<MyCodeView> { | ||
double _textScaleFactor = 1.0; | ||
|
||
Widget _getCodeView(String codeContent, BuildContext context) { | ||
final SyntaxHighlighterStyle style = | ||
Theme.of(context).brightness == Brightness.dark | ||
? SyntaxHighlighterStyle.darkThemeStyle() | ||
: SyntaxHighlighterStyle.lightThemeStyle(); | ||
// TODO: try out CustomScrollView and SliverAppbar (appbar auto hides when scroll). | ||
return Stack( | ||
alignment: AlignmentDirectional.bottomEnd, | ||
children: <Widget>[ | ||
Scrollbar( | ||
child: SingleChildScrollView( | ||
child: SingleChildScrollView( | ||
scrollDirection: Axis.horizontal, | ||
child: RichText( | ||
textScaleFactor: this._textScaleFactor, | ||
text: TextSpan( | ||
style: TextStyle(fontFamily: 'monospace', fontSize: 12.0), | ||
children: <TextSpan>[ | ||
DartSyntaxHighlighter(style).format(codeContent) | ||
], | ||
), | ||
), | ||
), | ||
), | ||
), | ||
Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: <Widget>[ | ||
IconButton( | ||
icon: Icon(Icons.zoom_out), | ||
onPressed: () => setState(() { | ||
this._textScaleFactor = | ||
max(0.8, this._textScaleFactor - 0.1); | ||
}), | ||
), | ||
IconButton( | ||
icon: Icon(Icons.zoom_in), | ||
onPressed: () => setState(() { | ||
this._textScaleFactor += 0.1; | ||
}), | ||
), | ||
], | ||
), | ||
], | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
// Loading string from file returns a Future<String>, so instead of returning directly the | ||
// widget, we need a FutureBuilder. | ||
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 Padding( | ||
padding: EdgeInsets.all(4.0), | ||
child: _getCodeView(snapshot.data, context), | ||
); | ||
} else { | ||
return Center(child: CircularProgressIndicator()); | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,128 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'show_code.dart'; | ||
|
||
class TestHttp extends StatefulWidget { | ||
final String url, method; | ||
final String url; | ||
|
||
TestHttp({String url, method}):url = url, method = method; | ||
TestHttp({String url}):url = url; | ||
|
||
@override | ||
State<StatefulWidget> createState() => TestHttpState(); | ||
}//TestHttp | ||
|
||
class ListTestHttp extends StatelessWidget { | ||
final Widget _title; | ||
final Widget _content; | ||
|
||
ListTestHttp({Widget title,content}): _title = title, _content = content; | ||
|
||
Widget _construct() { | ||
if (_title != null && _content != null) { | ||
return Column(children: <Widget>[_title, Expanded(child: _content)],); | ||
} | ||
|
||
return _content; | ||
}//_construct | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container(child: _construct(), height: 80.0, padding: EdgeInsets.all(10.0),); | ||
}//build | ||
}//ListTestHttp | ||
}// TestHttp | ||
|
||
class TestHttpState extends State<TestHttp> { | ||
final _formKey = GlobalKey<FormState>(); | ||
|
||
String _url, _method, _body; | ||
String _url, _body; | ||
int _status; | ||
|
||
@override | ||
void initState() { | ||
_url = widget.url; | ||
_method = widget.method; | ||
|
||
super.initState(); | ||
}//initState | ||
|
||
_sendRequestGet() { | ||
if(_formKey.currentState.validate()) { | ||
_formKey.currentState.save();//update form data | ||
|
||
http.get(_url).then((response){ | ||
_status = response.statusCode; | ||
_body = response.body; | ||
|
||
setState(() {});//reBuildWidget | ||
}).catchError((error){ | ||
_status = 0; | ||
_body = error.toString(); | ||
|
||
setState(() {});//reBuildWidget | ||
}); | ||
} | ||
}//_sendRequestGet | ||
|
||
_sendRequestPost() async { | ||
if(_formKey.currentState.validate()) { | ||
_formKey.currentState.save();//update form data | ||
|
||
try { | ||
var response = await http.post(_url); | ||
|
||
_status = response.statusCode; | ||
_body = response.body; | ||
} catch (error) { | ||
_status = 0; | ||
_body = error.toString(); | ||
} | ||
setState(() {});//reBuildWidget | ||
} | ||
}//_sendRequestPost | ||
|
||
_sendRequestPostBodyHeaders() async { | ||
if(_formKey.currentState.validate()) { | ||
_formKey.currentState.save();//update form data | ||
|
||
try { | ||
var response = await http.post(_url, | ||
body: {'name':'test','num':'10'}, | ||
headers: {'Accept':'application/json'} | ||
); | ||
|
||
_status = response.statusCode; | ||
_body = response.body; | ||
} catch (error) { | ||
_status = 0; | ||
_body = error.toString(); | ||
} | ||
setState(() {});//reBuildWidget | ||
} | ||
}//_sendRequestPost | ||
|
||
|
||
Widget build(BuildContext context) { | ||
return Form(key: _formKey, child: SingleChildScrollView(child: Column( | ||
children: <Widget>[ | ||
ListTestHttp( | ||
content: TextFormField(initialValue: _url, validator: (value){if (value.isEmpty) return 'API url isEmpty';}, onSaved: (value){_url = value;}, autovalidate: true), | ||
title: Text('API url', style: TextStyle(fontSize: 20.0,color: Colors.blue)) | ||
Container( | ||
child: Text('API url', style: TextStyle(fontSize: 20.0,color: Colors.blue)), | ||
padding: EdgeInsets.all(10.0) | ||
), | ||
ListTestHttp( | ||
content: TextFormField(initialValue: _method, validator: (value){if (value.isEmpty) return 'API method isEmpty';}, onSaved: (value){_method = value;}, autovalidate: true), | ||
title: Text('API method', style: TextStyle(fontSize: 20.0,color: Colors.blue)), | ||
Container( | ||
child: TextFormField(initialValue: _url, validator: (value){if (value.isEmpty) return 'API url isEmpty';}, onSaved: (value){_url = value;}, autovalidate: true), | ||
padding: EdgeInsets.all(10.0) | ||
), | ||
SizedBox(height: 20.0), | ||
RaisedButton( | ||
child: Text('Send request'), | ||
onPressed: () async { | ||
if(_formKey.currentState.validate()) { | ||
_formKey.currentState.save(); | ||
|
||
try { | ||
var response = await http.get('$_url$_method'); | ||
|
||
_status = response.statusCode; | ||
_body = response.body; | ||
|
||
|
||
print("Response status: ${_status}"); | ||
print("Response body: ${_body}"); | ||
|
||
} catch (error) { | ||
print(error); | ||
} | ||
|
||
setState(() { | ||
}); | ||
|
||
|
||
} | ||
} | ||
), | ||
RaisedButton(child: Text('Send request GET'), onPressed: _sendRequestGet), | ||
RaisedButton(child: Text('Send request POST'), onPressed: _sendRequestPost), | ||
RaisedButton(child: Text('Send request POST with Body and Headers'), onPressed: _sendRequestPostBodyHeaders), | ||
SizedBox(height: 20.0), | ||
|
||
Text('Response status', style: TextStyle(fontSize: 20.0,color: Colors.blue)), | ||
Text(_status == null ? '' :_status.toString()), | ||
|
||
SizedBox(height: 20.0), | ||
|
||
Text('Response body', style: TextStyle(fontSize: 20.0,color: Colors.blue)), | ||
Text(_body == null ? '' : _body), | ||
], | ||
))); | ||
}//build | ||
}//TestHttpState | ||
|
||
class MyApp extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('Test HTTP API'), | ||
actions: <Widget>[IconButton(icon: Icon(Icons.code), tooltip: 'Code', onPressed: (){ | ||
Navigator.push(context, MaterialPageRoute(builder: (context) => CodeScreen())); | ||
})], | ||
), | ||
body: TestHttp(url: 'https://json.flutter.su/echo') | ||
); | ||
} | ||
} | ||
|
||
void main() => runApp( | ||
new MaterialApp( | ||
MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
home: new Scaffold( | ||
appBar: new AppBar(title: new Text('Test HTTP API')), | ||
body: new TestHttp(url: 'https://json.flutter.su', method: '/echo') | ||
) | ||
home: MyApp() | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'code_view.dart'; | ||
|
||
class CodeScreen extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text('Source code')), | ||
body: MyCodeView(filePath: 'lib/main.dart') | ||
); | ||
} | ||
} |
Oops, something went wrong.