Skip to content

Commit 04845c7

Browse files
committed
拆分pages和widgets目录,抽离路由配置
1 parent 1fd99fc commit 04845c7

File tree

7 files changed

+136
-105
lines changed

7 files changed

+136
-105
lines changed

lib/config.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
22

33
const appConfig = {'title': 'flutter个人博客模板'};
44

5+
const baseUrl = 'http://192.168.1.137:3001';
6+
57
const primaryColor = const MaterialColor(
68
0xFF66BB6A,
79
<int, Color>{
@@ -34,5 +36,3 @@ const lightGreen = const MaterialColor(0xFFE8F5E9, <int, Color>{
3436
const lightGrey = const MaterialColor(0xf7f7f7f7, <int, Color>{
3537
50: Color(0xf7f7f7f7),
3638
});
37-
38-
const baseUrl = 'http://192.168.1.135:3001';

lib/main.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import 'package:blog_flutter/widgets/article.dart';
2-
import 'package:blog_flutter/widgets/home.dart';
1+
import 'package:blog_flutter/router.dart';
32
import 'package:flutter/material.dart';
4-
import './widgets/home.dart';
53
import 'config.dart';
64

75
void main() {
@@ -15,10 +13,7 @@ class App extends StatelessWidget {
1513
theme: ThemeData(
1614
primarySwatch: primaryColor,
1715
),
18-
routes: {
19-
'/home': (context) => HomeWidget(),
20-
'/article': (context) => ArticleWidget()
21-
},
22-
home: HomeWidget());
16+
routes: routerMap,
17+
home: homeWidget);
2318
}
2419
}
File renamed without changes.

lib/pages/form.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:flutter/material.dart';
2+
3+
class FormWidget extends StatelessWidget {
4+
final String title;
5+
6+
FormWidget({Key key, @required this.title}) : super(key: key);
7+
8+
@override
9+
Widget build(BuildContext context) {
10+
return Scaffold(
11+
appBar: AppBar(
12+
title: Text(title),
13+
),
14+
body: Form(
15+
child: FormField(builder: (context) {
16+
return Text('sasas');
17+
}),
18+
),
19+
);
20+
}
21+
}

lib/pages/home.dart

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import 'package:blog_flutter/config.dart';
2+
import 'package:blog_flutter/http.dart';
3+
import 'package:flutter/material.dart';
4+
5+
class HomeWidget extends StatefulWidget {
6+
@override
7+
HomeWidgetState createState() => new HomeWidgetState();
8+
}
9+
10+
class HomeWidgetState extends State<HomeWidget> {
11+
var _articles = <dynamic>[];
12+
var _start = 0, _limit = 10, _total = 0;
13+
14+
@override
15+
void initState() {
16+
super.initState();
17+
_getArticles();
18+
}
19+
20+
void _getArticles() async {
21+
var response = await http
22+
.get('/articles', queryParameters: {'start': _start, 'limit': _limit});
23+
setState(() {
24+
_articles.addAll(response.data['data']);
25+
_total = response.data['total'];
26+
});
27+
}
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return Scaffold(
32+
appBar: AppBar(
33+
title: Center(
34+
child: Text(
35+
appConfig['title'],
36+
style: TextStyle(color: Colors.white),
37+
)),
38+
),
39+
body: ListView.separated(
40+
itemCount: _articles.length,
41+
itemBuilder: (context, index) {
42+
final backgroundColor = index % 2 == 0 ? lightGreen : lightGrey;
43+
44+
if (_articles.length == 0 || index == _articles.length - 1) {
45+
if (_articles.length < _total - 1) {
46+
_start += _limit;
47+
_getArticles();
48+
return Container(
49+
padding: EdgeInsets.all(16),
50+
alignment: Alignment.center,
51+
decoration: BoxDecoration(color: backgroundColor),
52+
child: SizedBox(
53+
width: 24,
54+
height: 24,
55+
child: CircularProgressIndicator(
56+
strokeWidth: 2,
57+
)),
58+
);
59+
} else {
60+
return Container(
61+
padding: EdgeInsets.all(16),
62+
alignment: Alignment.center,
63+
decoration: BoxDecoration(color: backgroundColor),
64+
child: Text('没有更多了', style: TextStyle(color: Colors.black87)),
65+
);
66+
}
67+
}
68+
69+
return Container(
70+
decoration: BoxDecoration(color: backgroundColor),
71+
child: ListTile(
72+
title: Text(_articles[index]['title'],
73+
overflow: TextOverflow.ellipsis,
74+
style: TextStyle(
75+
fontSize: 22, height: 1.5, color: Colors.green)),
76+
subtitle: Text(
77+
_articles[index]['content'],
78+
overflow: TextOverflow.ellipsis,
79+
maxLines: 2,
80+
style: TextStyle(fontSize: 16, height: 1.5),
81+
),
82+
onTap: () => Navigator.of(context)
83+
.pushNamed('/article', arguments: _articles[index]),
84+
),
85+
);
86+
},
87+
separatorBuilder: (context, index) => Divider(
88+
height: 0,
89+
),
90+
),
91+
floatingActionButton: FloatingActionButton(
92+
child: Icon(Icons.add),
93+
onPressed: () =>
94+
Navigator.of(context).pushNamed('/form', arguments: '新增文章'),
95+
),
96+
);
97+
}
98+
}

lib/router.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:blog_flutter/pages/article.dart';
2+
import 'package:blog_flutter/pages/form.dart';
3+
import 'package:blog_flutter/pages/home.dart';
4+
import 'package:flutter/cupertino.dart';
5+
6+
final Widget homeWidget = HomeWidget();
7+
8+
final Map<String, WidgetBuilder> routerMap = {
9+
'/home': (context) => HomeWidget(),
10+
'/article': (context) => ArticleWidget(),
11+
'/form': (context) => FormWidget()
12+
};

lib/widgets/home.dart

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)