Skip to content

Commit 0494a2e

Browse files
1. add layout sample 2. add http request example 3. add unit test and widget test use case
1 parent 6d2deac commit 0494a2e

13 files changed

+1048
-739
lines changed

.packages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by pub on 2018-01-26 17:38:06.509533.
1+
# Generated by pub on 2018-02-26 14:42:35.603887.
22
analyzer:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.30.0+4/lib/
33
args:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.7/lib/
44
async:file:///C:/Users/zhangjun13/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.13.3/lib/

images/lake.jpg

272 KB
Loading

lib/http_request.dart

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:flutter/material.dart';
5+
import 'package:flutter/services.dart';
6+
import 'key_code.dart';
7+
8+
void main() {
9+
runApp(new MyApp());
10+
}
11+
12+
class MyApp extends StatelessWidget {
13+
@override
14+
Widget build(BuildContext context) {
15+
return new MaterialApp(
16+
home: new MyHomePage(),
17+
);
18+
}
19+
}
20+
21+
class MyHomePage extends StatefulWidget {
22+
MyHomePage({Key key}) : super(key: key);
23+
24+
@override
25+
_MyHomePageState createState() => new _MyHomePageState();
26+
}
27+
28+
class _MyHomePageState extends State<MyHomePage> {
29+
var _ipAddress = 'Unknown';
30+
FocusNode focusNode;
31+
bool _active = false;
32+
33+
_getIPAddress() async {
34+
print("_active = $_active");
35+
var url = 'https://httpbin.org/ip';
36+
var httpClient = new HttpClient();
37+
38+
String result;
39+
try {
40+
var request = await httpClient.getUrl(Uri.parse(url));
41+
var response = await request.close();
42+
if (response.statusCode == HttpStatus.OK) {
43+
var json = await response.transform(UTF8.decoder).join();
44+
var data = JSON.decode(json);
45+
result = data['origin'];
46+
} else {
47+
result =
48+
'Error getting IP address:\nHttp status ${response.statusCode}';
49+
}
50+
} catch (exception) {
51+
result = 'Failed getting IP address';
52+
}
53+
54+
// If the widget was removed from the tree while the message was in flight,
55+
// we want to discard the reply rather than calling setState to update our
56+
// non-existent appearance.
57+
if (!mounted) return;
58+
59+
setState(() {
60+
_ipAddress = result;
61+
_active = !_active;
62+
});
63+
}
64+
65+
@override
66+
void initState() {
67+
print('initState called.');
68+
super.initState();
69+
focusNode = new FocusNode();
70+
}
71+
72+
@override
73+
void dispose() {
74+
print('dispose called.');
75+
focusNode.dispose();
76+
super.dispose();
77+
}
78+
79+
@override
80+
Widget build(BuildContext context) {
81+
var spacer = new SizedBox(height: 32.0);
82+
FocusScope.of(context).requestFocus(focusNode);
83+
return new Scaffold(
84+
body: new Center(
85+
child: new Column(
86+
mainAxisAlignment: MainAxisAlignment.center,
87+
children: <Widget>[
88+
new Text('Your current IP address is:'),
89+
new Text('$_ipAddress.'),
90+
spacer,
91+
new RawKeyboardListener(
92+
focusNode: focusNode,
93+
onKey: (RawKeyEvent event) {
94+
if (event is RawKeyDownEvent &&
95+
event.data is RawKeyEventDataAndroid) {
96+
RawKeyDownEvent rawKeyDownEvent = event;
97+
RawKeyEventDataAndroid rawKeyEventDataAndroid =
98+
rawKeyDownEvent.data;
99+
print("keyCode = ${rawKeyEventDataAndroid.keyCode}");
100+
switch (rawKeyEventDataAndroid.keyCode) {
101+
case KEY_CENTER:
102+
_getIPAddress();
103+
break;
104+
default:
105+
break;
106+
}
107+
}
108+
},
109+
child: new RaisedButton(
110+
onPressed: _getIPAddress,
111+
color: _active ? Colors.lightGreen[700] : Colors.grey[600],
112+
child: new Text('Get IP address'),
113+
),
114+
),
115+
],
116+
),
117+
),
118+
);
119+
}
120+
}

lib/layout_demo.dart

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
7+
// Uncomment lines 7 and 10 to view the visual layout at runtime.
8+
//import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
9+
10+
void main() {
11+
//debugPaintSizeEnabled = true;
12+
runApp(new MyApp());
13+
}
14+
15+
class FavoriteWidget extends StatefulWidget {
16+
@override
17+
_FavoriteWidgetState createState() => new _FavoriteWidgetState();
18+
}
19+
20+
class _FavoriteWidgetState extends State<FavoriteWidget> {
21+
bool _isFavorited = true;
22+
int _favoriteCount = 41;
23+
24+
void _toggleFavorite() {
25+
setState(() {
26+
// If the lake is currently favorited, unfavorite it.
27+
if (_isFavorited) {
28+
_favoriteCount -= 1;
29+
_isFavorited = false;
30+
// Otherwise, favorite it.
31+
} else {
32+
_favoriteCount += 1;
33+
_isFavorited = true;
34+
}
35+
});
36+
}
37+
38+
@override
39+
Widget build(BuildContext context) {
40+
return new Row(
41+
mainAxisSize: MainAxisSize.min,
42+
children: [
43+
new Container(
44+
padding: new EdgeInsets.all(0.0),
45+
child: new IconButton(
46+
icon: (_isFavorited
47+
? new Icon(Icons.star)
48+
: new Icon(Icons.star_border)),
49+
color: Colors.red[500],
50+
onPressed: _toggleFavorite,
51+
),
52+
),
53+
new SizedBox(
54+
width: 18.0,
55+
child: new Container(
56+
child: new Text('$_favoriteCount'),
57+
),
58+
),
59+
],
60+
);
61+
}
62+
}
63+
64+
class MyApp extends StatelessWidget {
65+
@override
66+
Widget build(BuildContext context) {
67+
Widget titleSection = new Container(
68+
padding: const EdgeInsets.all(32.0),
69+
child: new Row(
70+
children: [
71+
new Expanded(
72+
child: new Column(
73+
crossAxisAlignment: CrossAxisAlignment.start,
74+
children: [
75+
new Container(
76+
padding: const EdgeInsets.only(bottom: 8.0),
77+
child: new Text(
78+
'Oeschinen Lake Campground',
79+
style: new TextStyle(
80+
fontWeight: FontWeight.bold,
81+
),
82+
),
83+
),
84+
new Text(
85+
'Kandersteg, Switzerland',
86+
style: new TextStyle(
87+
color: Colors.grey[500],
88+
),
89+
),
90+
],
91+
),
92+
),
93+
new FavoriteWidget(),
94+
],
95+
),
96+
);
97+
98+
Column buildButtonColumn(IconData icon, String label) {
99+
Color color = Theme
100+
.of(context)
101+
.primaryColor;
102+
103+
return new Column(
104+
mainAxisSize: MainAxisSize.min,
105+
mainAxisAlignment: MainAxisAlignment.center,
106+
children: [
107+
new Icon(icon, color: color),
108+
new Container(
109+
margin: const EdgeInsets.only(top: 8.0),
110+
child: new Text(
111+
label,
112+
style: new TextStyle(
113+
fontSize: 12.0,
114+
fontWeight: FontWeight.w400,
115+
color: color,
116+
),
117+
),
118+
),
119+
],
120+
);
121+
}
122+
123+
Widget buttonSection = new Container(
124+
child: new Row(
125+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
126+
children: [
127+
buildButtonColumn(Icons.call, 'CALL'),
128+
buildButtonColumn(Icons.near_me, 'ROUTE'),
129+
buildButtonColumn(Icons.share, 'SHARE'),
130+
],
131+
),
132+
);
133+
134+
Widget textSection = new Container(
135+
padding: const EdgeInsets.all(32.0),
136+
child: new Text(
137+
'''
138+
Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese Alps. Situated 1,578 meters above sea level, it is one of the larger Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour walk through pastures and pine forest, leads you to the lake, which warms to 20 degrees Celsius in the summer. Activities enjoyed here include rowing, and riding the summer toboggan run.
139+
''',
140+
softWrap: true,
141+
),
142+
);
143+
return new MaterialApp(
144+
title: 'Flutter Demo',
145+
home: new Scaffold(
146+
appBar: new AppBar(
147+
title: new Text('Top Lakes'),
148+
),
149+
body: new ListView(
150+
children: [
151+
new Image.asset(
152+
'images/lake.jpg',
153+
height: 240.0,
154+
fit: BoxFit.cover,
155+
),
156+
titleSection,
157+
buttonSection,
158+
textSection,
159+
],
160+
),
161+
),
162+
);
163+
}
164+
}

lib/navigator_demo.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ class _MyHomePageState extends State<MyHomePage> {
186186
new Container(
187187
alignment: Alignment.center,
188188
margin: const EdgeInsets.all(5.0),
189-
child:
190-
new RawKeyboardListener(
189+
child: new RawKeyboardListener(
191190
focusNode: focusNode2,
192191
onKey: (RawKeyEvent event) {
193192
if (event is RawKeyDownEvent &&

0 commit comments

Comments
 (0)