flutter - 如何使用本地 JSON 在我的应用程序中动态创建页面?
问题描述
我有一个格式为 JSON 的文件:
{
"Name": "Hello John",
"Description": "Profile Text",
"URL": "//apply now link ",
"Orientation": "Landscape",
"1024x500": "//profile picture link",
},
现在,当我的 gridView iOS 中的特定图块单击时,我想创建如下所示的页面。例如。如果用户单击 Hello John Tile 我想创建页面(显示在图像中),该页面将具有 Hello John 的标题和 John 的图像(链接 3)以及打开 Johns 个人资料的 url。但是 json 文件有 200 多个这样的配置文件,我想动态创建一个页面,当单击相应的 gridView Tile 时将打开该页面。还有一个需要从 JSON 文件调用的方向要求。
这是网格视图。网格视图已编入索引并具有标题和图像。只有下一页必须使用 Json 调用。
这是页面的代码:
class CourseInfoScreen extends StatefulWidget {
@override
_CourseInfoScreenState createState() => _CourseInfoScreenState();
}
class _CourseInfoScreenState extends State<CourseInfoScreen>
with TickerProviderStateMixin {
Future launchURL(String url) async {
const url = '//apply now link ';
if (await canLaunch(url)) {
await launch(url, forceWebView: true, forceSafariVC: true);
} else {
throw 'Could not launch $url';
}
}
Widget build(BuildContext context) {
final double tempHeight = MediaQuery.of(context).size.height -
(MediaQuery.of(context).size.width / 1.2) +
24.0;
return Container(
color: DesignCourseAppTheme.nearlyWhite,
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image(
image: NetworkImage(
//if(index==1){}
"https://image.shutterstock.com/image-vector/young-man-face-angry-facial-260nw-510023299.jpg",
),
fit: BoxFit.fill, // use this
),
),
)
],
),
Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
bottom: 18,
top: 16
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Hello John',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: "Netflix",
fontWeight: FontWeight.w600,
fontSize: 35,
letterSpacing: 0.27,
color: HexColor('FFFF8C3B')
),
),
],
),
),
Expanded(
child: GestureDetector(
onTap: () {
print('pressed');
_interstitialAd?.show();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => WebviewScaffold(
url:"//apply now link to be inserted here",
//details of the Webview Scafflod
appBar: AppBar(
leading: new IconButton(
icon: new Icon(
Icons.arrow_back),
onPressed: () {
Navigator.pop(
context);
},
),
backgroundColor:
Color(0xFFFF8C3B),
title: const Text(
"Greedy Gnomes",
),
),
child: Center(
child: Text(
'Apply Now',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: "Netflix",
fontWeight: FontWeight.w600,
fontSize: 18,
letterSpacing: 0.0,
color: Colors.white,
),
),
),
}
解决方案
这是一个示例,使用您的数据而不是您的布局,说明如何获取数据,将其列在网格中,然后点击每个项目移动到该项目的详细视图:
class PageFromJson extends StatefulWidget {
@override
_PageFromJsonState createState() => _PageFromJsonState();
}
class _PageFromJsonState extends State<PageFromJson> {
List<Map> itemList = [
{
"Name": "Hello John",
"Description": "Profile Text",
"URL": "//apply now link ",
"Orientation": "Landscape",
"1024x500": "//profile picture link",
},
{
"Name": "Hello Ana",
"Description": "Profile Text",
"URL": "//apply now link ",
"Orientation": "Landscape",
"1024x500": "//profile picture link",
},
{
"Name": "Hello Alice",
"Description": "Profile Text",
"URL": "//apply now link ",
"Orientation": "Landscape",
"1024x500": "//profile picture link",
},
];
@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: itemList.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () => _goToItemViewer(itemList[index]),
child: ItemCard(
item: itemList[index]
),
);
},
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
);
}
void _goToItemViewer(item){
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return ItemViewer(item: item);
}));
}
}
class ItemCard extends StatelessWidget {
final Map item;
ItemCard({
@required this.item,
});
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
color: Colors.blueGrey,
margin: EdgeInsets.all(12),
height: 200,
width: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(item['Name']),
Text(item['1024x500']),
],
),
);
}
}
class ItemViewer extends StatelessWidget {
final Map item;
ItemViewer({
@required this.item,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(item['Name']),
),
body: Center(
child: Column(
children: <Widget>[
Text(item['Name']),
Text(item['Description']),
Text(item['URL']),
Text(item['Orientation']),
Text(item['1024x500']),
],
),
),
);
}
}
推荐阅读
- c++ - C ++程序查找增加的峰值数量
- angular - 无法让 ColorThief 在 Angular 中正常工作,找不到库方法?
- angular - 在 Angular 应用程序中对 /index.html 进行 POST 调用时出现 500 内部服务器错误
- javascript - Jquery html 函数在调用 remove 之前无法正常工作
- python - 如何删除已删除的小部件的空白空间?
- javascript - 在下一个 js 中使用路由器替换
- php - 性能如何在具有外部数据库连接的 PHP websocket 中发挥作用?
- pine-script - TradingView pine-script 收益日之间的最高和最低
- python - Python zipfile 添加了不必要的父文件夹
- centos7 - 多ip和参数测试