Skip to content

Commit

Permalink
新增列表和详情页
Browse files Browse the repository at this point in the history
  • Loading branch information
OMGVecchio committed Nov 28, 2018
1 parent 0591936 commit bb7bd3d
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
108 changes: 9 additions & 99 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,111 +1,21 @@
import 'package:flutter/material.dart';

void main() => runApp(MyApp());
import 'pages/article_list.dart';
// import 'pages/article_detail.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
primaryColor: Colors.pink,
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
home: ArticleListPage(),
routes: <String, WidgetBuilder> {
// '/article': (BuildContext ctx) => ArticleDetailPage(),
},
);
}
}
91 changes: 91 additions & 0 deletions lib/pages/article_detail.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
// import 'package:flutter_markdown/flutter_markdown.dart';

Dio dio = Dio();

class ArticleDetailPage extends StatelessWidget {
final articleId;
ArticleDetailPage(this.articleId);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('文章详情'),
),
body: ArticleDetail(this.articleId),
);
}
}

class ArticleDetail extends StatefulWidget {
final articleId;
ArticleDetail(this.articleId);
@override
createState() => ArticleDetailState(this.articleId);
}

class ArticleDetailState extends State {
final articleId;
Map articleDetail;
ArticleDetailState(this.articleId) {
this._fetchArticleDetail();
}
void _fetchArticleDetail() async {
// Response res = await dio.get('https://vecchio.top/api/article/${this.articleId}');
Response res = await dio.get('https://vecchio.top/api/article/f24be360-9bac-11e8-af01-d933a8a0c468');
setState(() {
articleDetail = res.data['data'];
});
}
@override
Widget build(BuildContext context) {
if (articleDetail == null) {
return Center(
child: CircularProgressIndicator(),
);
}
return Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Center(
child: Text(
articleDetail['title'],
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.w700,
),
),
),
Container(
margin: EdgeInsets.only(top: 8.0),
child: Row(
textDirection: TextDirection.rtl,
children: <Widget>[
Text(DateTime.fromMillisecondsSinceEpoch(articleDetail['createTime']).toString()),
],
),
),
Container(
margin: EdgeInsets.fromLTRB(0, 8.0, 0, 20.0),
child: Row(
children: <Widget>[
Text(
articleDetail['desc'],
),
],
),
),
Expanded(
child: Container(
child: Text(
articleDetail['article'].toString(),
),
),
),
],
),
);
}
}
77 changes: 77 additions & 0 deletions lib/pages/article_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import 'article_detail.dart';

Dio dio = Dio();
const String defaultCoverUrl = 'https://vecchio.top/images/cover/wallhaven-851_1537242430407.jpg';

class ArticleListPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('文章列表'),
),
body: ArticleList(),
);
}
}

class ArticleList extends StatefulWidget {
@override
createState() => ArticleListState();
}

class ArticleListState extends State {
final articleList = [];
ArticleListState() {
this._fetchArticleList();
}
void _fetchArticleList() async {
Response res = await dio.get('https://vecchio.top/api/article');
setState(() {
articleList.addAll(res.data['data']);
});
}
@override
Widget build(BuildContext context) {
return ListView.builder(
padding: EdgeInsets.all(16.0),
itemCount: articleList.length,
itemBuilder: (ctx, i) {
if (i < articleList.length) {
final articleInfo = articleList[i];
final coverUrl = articleInfo['cover'];
final articleCover = coverUrl != null ? ('https://vecchio.top${coverUrl}') : defaultCoverUrl;
return Card(
child: Column(
children: <Widget>[
Center(
child: Image.network(
articleCover,
fit: BoxFit.fitHeight,
height: 200,
),
),
ListTile(
title: Text(articleInfo['title']),
subtitle: Text(articleInfo['desc']),
onTap: () {
// Navigator.pushNamed(context, '/article');
Navigator.push(
context,
MaterialPageRoute(
builder: (ctx) => ArticleDetailPage(articleInfo['id']),
),
);
},
),
],
),
);
}
},
);
}
}
6 changes: 6 additions & 0 deletions lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Utils {
static formatTime (int timeStamp) {
DateTime dt = new DateTime.fromMillisecondsSinceEpoch(timeStamp);
return dt.toString();
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ environment:
dependencies:
flutter:
sdk: flutter
dio: ^1.0.6
flutter_markdown: ^0.2.0

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit bb7bd3d

Please sign in to comment.