import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_weight_ui/model/home_article_data_entity.dart';
import 'package:flutter_weight_ui/net/RequestUtils.dart';
class ArticlePage extends StatefulWidget{
@override
State<StatefulWidget> createState() =>_ArticlePage();
}
class _ArticlePage extends State<ArticlePage>{
int page=0;
static const myPlugin = const MethodChannel("samples.flutter.io/webview");//通道与Android原生交互
List<HomeArticleDataDataData> articleList = [];
ScrollController _scrollController = ScrollController();//listView的控制器
@override
void initState() {
getArticleList(page);
_scrollController.addListener(() {
if(_scrollController.position.pixels==_scrollController.position.maxScrollExtent){//如果是最后一个item则触发,加载新数据
page++;
getArticleList(page);
}
});
super.initState();
}
getArticleList(int page) async {
var result = await RequestManager.request("https://www.wanandroid.com/article/list/$page/json");
setState(() {
HomeArticleDataEntity dataEntity = HomeArticleDataEntity().fromJson(result);
if(page==0){
articleList.clear();
}
articleList.addAll(dataEntity.data.datas);
});
}
/// 下拉刷新方法
Future<Null> _onRefresh() async{
page=0;
getArticleList(page);
}
///加载更多
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("文章"),
),
body:
RefreshIndicator(
onRefresh: _onRefresh,
child: ListView.builder(
itemCount: articleList.length,
controller: _scrollController,
itemBuilder:(BuildContext context,int index){
return Container(
margin:EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.white70,
border: Border.all(color: Colors.white, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(12.0))
),
child:
GestureDetector(
onTap: (){
Map<String, String> map = {"url": articleList[index].link};
myPlugin.invokeMethod("setWebViewUrl",map);//把url传递给原生页面
},
child:ConstrainedBox(//需要包上一层,否则会报错
constraints: BoxConstraints.tightForFinite(width: double.infinity,height: 120.0),
child: Stack(
alignment:Alignment.center , //指定未定位或部分定位widget的对齐方式
children: <Widget>[
Positioned(
top: 8.0,
left: 8.0,
child: Text(articleList[index].shareUser==""?articleList[index].author:articleList[index].shareUser),
),
Positioned(
right: 8.0,
top: 8.0,
child: Text(articleList[index].niceShareDate),
),
Container(
padding: EdgeInsets.only(left: 8.0,top: 35.0,right: 8.0),
child:
Column(
children: <Widget>[
Expanded(//Expanded只能在Row Colum中,否则会有异常。如果不加ExpandedText无法实现自动换行
child:
Align(//如果不包一层Align,会出现文字换行第二行居中
alignment: Alignment.topLeft,
child:
Text(articleList[index].title,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15.0,
),
),
) ,
),
],
),
),
Positioned(
left: 8.0,
bottom: 16.0,
child: Text(articleList[index].superChapterName,style: TextStyle(
color: Colors.red
),),
),
Positioned(
right: 8.0,
bottom: 16.0,
child: Image(
width: 15.0,
height: 15.0,
image: AssetImage("assets/images/love.png"),
),
),
],
),
) ,
),
);
},
),
),
);
}
}
网络请求的类可以去上一篇文章找,中间遇见的问题Text无法自动换行,加上Expand包一层就好,又遇见第二个问题,换行之后第二行居中,加了一层Align就好了。中间有Flutter把webUrl传递给原生,原生的代码如下:
package com.example.flutter_weight_ui
import android.content.Intent
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
private val CHANNEL = "samples.flutter.io/webview"//通道名称
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(FlutterEngine(this))
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if(call.method=="setWebViewUrl"){//flutter方法名称
var url:String = call.argument("url")!!
var intent = Intent(this,WebViewActivity::class.java).putExtra("url", url)
startActivity(intent)
}
}
}
}
package com.example.flutter_weight_ui
import android.app.Activity
import android.os.Bundle
import android.webkit.WebView
class WebViewActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
var webview:WebView = findViewById(R.id.webview);
webview.loadUrl(intent.getStringExtra("url"))
}
}
在清单文件中注册一下