Skip to content

Commit be57ab6

Browse files
author
unknown
committed
update
1 parent b796326 commit be57ab6

31 files changed

+2347
-0
lines changed

projects/lib/main2.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import 'package:flutter/material.dart';
2+
3+
void main() => runApp(new MyApp());
4+
5+
class MyApp extends StatelessWidget {
6+
// This widget is the root of your application.
7+
@override
8+
Widget build(BuildContext context) {
9+
return new MaterialApp(
10+
title: 'Flutter Demo',
11+
theme: new ThemeData(
12+
primarySwatch: Colors.blue,
13+
),
14+
home: new MyHomePage(title: 'Flutter Demo Home Page'),
15+
);
16+
}
17+
}
18+
19+
class MyHomePage extends StatefulWidget {
20+
final String title;
21+
MyHomePage({Key key, this.title}) : super(key: key);
22+
23+
@override
24+
_MyHomePageState createState() => new _MyHomePageState();
25+
}
26+
27+
class _MyHomePageState extends State<MyHomePage> {
28+
int _counter = 0;
29+
30+
void _incrementCounter() {
31+
setState(() {
32+
_counter++;
33+
});
34+
}
35+
36+
@override
37+
Widget build(BuildContext context) {
38+
return new Scaffold(
39+
appBar: new AppBar(
40+
title: new Text(widget.title),
41+
),
42+
body: new Center(
43+
child: new Column(
44+
mainAxisAlignment: MainAxisAlignment.center,
45+
children: <Widget>[
46+
new Text(
47+
'You have pushed the button this many times:',
48+
),
49+
new Text(
50+
'$_counter',
51+
style: Theme.of(context).textTheme.display1,
52+
),
53+
],
54+
),
55+
),
56+
floatingActionButton: new FloatingActionButton(
57+
onPressed: _incrementCounter,
58+
tooltip: 'Increment',
59+
child: new Icon(Icons.add),
60+
),
61+
);
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// 交互
4+
void main() {
5+
runApp(new MaterialApp(
6+
title: '交互',
7+
home: new Counter(),
8+
));
9+
}
10+
11+
class Counter extends StatefulWidget {
12+
@override
13+
_CounterState createState() => new _CounterState();
14+
}
15+
16+
class _CounterState extends State<Counter> {
17+
int _count = 0; //计数
18+
19+
void _increment() {
20+
setState(() {
21+
_count += 1;
22+
});
23+
}
24+
25+
@override
26+
Widget build(BuildContext context) {
27+
return new Scaffold(
28+
appBar: new AppBar(
29+
title: new Text('交互'),
30+
),
31+
body: new Center(
32+
child: new Text('按钮点击 $_count 次}'),
33+
),
34+
floatingActionButton: new FloatingActionButton(
35+
onPressed: _increment,
36+
tooltip: '增加',
37+
child: new Icon(Icons.add),
38+
),
39+
);
40+
}
41+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'package:flutter/material.dart';
2+
3+
void main() {
4+
runApp(new MaterialApp(
5+
title: '交互',
6+
home: new Counter(),
7+
));
8+
}
9+
10+
class Counter extends StatefulWidget {
11+
@override
12+
_CounterState createState() => new _CounterState();
13+
}
14+
15+
class _CounterState extends State<Counter> {
16+
int _count = 0; //计数
17+
18+
void _increment() {
19+
setState(() {
20+
_count += 1;
21+
});
22+
}
23+
24+
@override
25+
Widget build(BuildContext context) {
26+
return new Scaffold(
27+
appBar: new AppBar(
28+
title: new Text('交互'),
29+
),
30+
body: new CounterDiaplay(count: _count),
31+
floatingActionButton: new CounterIncrementer(
32+
onPressed: _increment,
33+
),
34+
);
35+
}
36+
}
37+
38+
// 简单的封装 计数显示
39+
class CounterDiaplay extends StatelessWidget {
40+
CounterDiaplay({this.count});
41+
42+
final int count;
43+
44+
@override
45+
Widget build(BuildContext context) {
46+
return new Center(
47+
child: new Text('按钮点击 $count 次}'),
48+
);
49+
}
50+
}
51+
52+
// 简单的封装 计数改变
53+
class CounterIncrementer extends StatelessWidget {
54+
CounterIncrementer({this.onPressed});
55+
56+
final VoidCallback onPressed;
57+
58+
@override
59+
Widget build(BuildContext context) {
60+
return new FloatingActionButton(
61+
onPressed: onPressed,
62+
tooltip: '增加',
63+
child: new Icon(Icons.add),
64+
);
65+
}
66+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import 'package:flutter/material.dart';
2+
import 'ShoppingListItem.dart';
3+
4+
/// ListView 购物清单 demo
5+
void main() {
6+
runApp(
7+
new MaterialApp(
8+
title: 'Flutter教程',
9+
home: new ShoppingList(products: _kProducts),
10+
),
11+
);
12+
}
13+
14+
// 创建Products集合
15+
final List<Product> _kProducts = <Product>[
16+
new Product(name: '番茄'),
17+
new Product(name: '辣椒'),
18+
new Product(name: '茄子'),
19+
new Product(name: '芥蓝'),
20+
new Product(name: '黄瓜'),
21+
new Product(name: '白菜'),
22+
new Product(name: '西蓝花'),
23+
new Product(name: '韭菜'),
24+
new Product(name: '莲藕'),
25+
new Product(name: '西芹'),
26+
new Product(name: '土豆'),
27+
new Product(name: '冬瓜'),
28+
new Product(name: '胡瓜'),
29+
new Product(name: '豇豆'),
30+
new Product(name: '四季豆'),
31+
new Product(name: '红萝卜'),
32+
new Product(name: '白萝卜'),
33+
new Product(name: '胡萝卜'),
34+
new Product(name: '豆腐'),
35+
new Product(name: '山药'),
36+
new Product(name: '洋葱'),
37+
new Product(name: '豆芽'),
38+
new Product(name: '金针菇'),
39+
new Product(name: '香菇'),
40+
];
41+
42+
class ShoppingList extends StatefulWidget {
43+
ShoppingList({Key key, this.products}) : super(key: key);
44+
final List<Product> products;
45+
46+
@override
47+
_ShoppingListState createState() => new _ShoppingListState();
48+
}
49+
50+
class _ShoppingListState extends State<ShoppingList> {
51+
Set<Product> _shoppingCart = new Set<Product>();
52+
53+
void _handleCartChanged(Product product, bool inCart) {
54+
setState(() {
55+
if (inCart) {
56+
_shoppingCart.add(product);
57+
} else {
58+
_shoppingCart.remove(product);
59+
}
60+
});
61+
}
62+
63+
@override
64+
Widget build(BuildContext context) {
65+
return new Scaffold(
66+
appBar: new AppBar(
67+
title: new Text('购物清单'),
68+
leading: new IconButton(
69+
icon: new Icon(Icons.menu), tooltip: '导航菜单', onPressed: null),
70+
),
71+
body: new ListView(
72+
children: widget.products.map((Product product) {
73+
return new ShoppingListItem(
74+
//监听
75+
product: product,
76+
inCart: _shoppingCart.contains(product),
77+
onCartChanged: _handleCartChanged,
78+
);
79+
}).toList(),
80+
),
81+
);
82+
}
83+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:fluttertoast/fluttertoast.dart';
3+
4+
class Product {
5+
const Product({this.name});
6+
7+
final String name;
8+
}
9+
10+
typedef void CartChangedCallback(Product p, bool inCart);
11+
12+
class ShoppingListItem extends StatelessWidget {
13+
ShoppingListItem({this.product, this.inCart, this.onCartChanged});
14+
15+
final Product product;
16+
final bool inCart;
17+
final CartChangedCallback onCartChanged;
18+
19+
Color _getColor(BuildContext context) {
20+
// 默认是主题颜色,选中状态是deepPurpleAccent
21+
return inCart
22+
? Colors.deepPurpleAccent
23+
: Theme.of(context).primaryColorDark;
24+
}
25+
26+
/// type: 用于区分是图形还是文字 0表示图形标识
27+
/// style:用于区分图形和文字哪个要绘制删除线,0表示需要给文字添加删除线。
28+
TextStyle _getTextStyle(BuildContext context, int type, int style) {
29+
if (!inCart) {
30+
//默认的状态
31+
return new TextStyle(
32+
color: type == 0 ? Colors.white : Theme.of(context).primaryColor,
33+
fontSize: 20.0,
34+
);
35+
} else {
36+
//选中状态
37+
return new TextStyle(
38+
color: type == 0 ? Colors.white : Colors.purpleAccent, //字体颜色
39+
decoration: style == 0 ? TextDecoration.lineThrough : null, //删除线
40+
decorationColor: style == 0 ? Colors.purple : null, //删除线颜色
41+
fontSize: 20.0,
42+
);
43+
}
44+
}
45+
46+
@override
47+
Widget build(BuildContext context) {
48+
return new ListTile(
49+
onTap: () {
50+
onCartChanged(product, !inCart);
51+
Fluttertoast.showToast(
52+
msg: "点击了:" + "${product.name}",
53+
toastLength: Toast.LENGTH_SHORT,
54+
gravity: ToastGravity.BOTTOM,
55+
);
56+
},
57+
leading: new CircleAvatar(
58+
//左边的圆形标识
59+
backgroundColor: _getColor(context), //背景颜色
60+
child: new Text(product.name[0], //文字内容
61+
style: _getTextStyle(context, 0, 1)), //文字格式
62+
),
63+
title: new Text(product.name, // 文字内容
64+
style: _getTextStyle(context, 1, 0)), //文字格式
65+
);
66+
}
67+
}

0 commit comments

Comments
 (0)