import ‘package:flutter/material.dart‘;
import ‘package:flutter_project/service/service_method.dart‘;
import ‘dart:convert‘;
import ‘swiper_demo.dart‘;
import ‘top_navigator.dart‘;
import ‘adbanner.dart‘;
import ‘callPhone.dart‘;
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(‘百姓生活‘)),
body: Container(
child: FutureBuilder(//网络数据返回能很好的渲染控件
future: getHomePageContent(),//网络返回的数据
builder: (context,snapshot){
if (snapshot.hasData) {//snapshot 相当于返回的数据
var data = json.decode(snapshot.data.toString());
List<Map> swiper = (data[‘data‘][‘slides‘] as List).cast();
List<Map> navlist = (data[‘data‘][‘category‘] as List).cast();
String adPic = data[‘data‘][‘advertesPicture‘][‘PICTURE_ADDRESS‘];
String phoneBgImage = data[‘data‘][‘shopInfo‘][‘leaderImage‘];
String phoneNum = data[‘data‘][‘shopInfo‘][‘leaderPhone‘];
return Column(
children: <Widget>[//各个组件
SwiperDiy(swiperDataList: swiper),//轮播
TopNavigator(navigatorList: navlist),//导航引导
AdBanner(adPicture: adPic),//广告
CallPhone(backgroundImage: phoneBgImage,shopPhone:phoneNum,),//打电话
],
);
}else{
return Center(
child: Text(‘加载错误‘),
);
}
},
),
),
);
}
}
打电话代码:
import ‘package:flutter/material.dart‘;
import ‘package:url_launcher/url_launcher.dart‘;//打电话插件 也可以打开网页 邮件
class CallPhone extends StatelessWidget {
final String backgroundImage;
final String shopPhone;
const CallPhone({Key key,this.backgroundImage,this.shopPhone}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: InkWell(//就是oc的手势
onTap: (){
_lanchURL();
},
child: Image.network(backgroundImage),
),
);
}
void _lanchURL() async{//需要真机测试
String url = ‘tel:‘ + shopPhone;
if (await canLaunch(url)) {
await launch(url);
}else{
throw ‘请检查是否是真机,URL不能访问‘;
}
}
}
总结: