Android studio 颤振应用程序不';除非单击某个按钮,否则无法显示内容
所以,我的Flitter应用程序不会显示通过API获取的内容,除非按下某个按钮,然后它只显示, 我不知道这里有什么问题,但很烦人, 我不知道是什么原因,所以下面是我的全部代码, 我是个新手,如果这是个愚蠢的错误,我很抱歉。 多谢各位Android studio 颤振应用程序不';除非单击某个按钮,否则无法显示内容,android-studio,flutter,dart,Android Studio,Flutter,Dart,所以,我的Flitter应用程序不会显示通过API获取的内容,除非按下某个按钮,然后它只显示, 我不知道这里有什么问题,但很烦人, 我不知道是什么原因,所以下面是我的全部代码, 我是个新手,如果这是个愚蠢的错误,我很抱歉。 多谢各位 import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:http/http.dart' as http; import 'package:FotoApp/image.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:FotoApp/image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
final Color myColor = Color(0xff222f3e);
final Color myColor2 = Color(0xff2f3640);
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(debug: true);
runApp(
MyApp(),
);
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static String query = "wallpapers";
List<dynamic> wallpapersList;
Icon searchIcon = Icon(Icons.search);
Widget searchBar = Text("FotoApp");
@override
void initState() {
super.initState();
initialize();
}
void initialize() async {
var apiUrl =
"https://api.pexels.com/v1/search?query=" + query + "&per_page=500";
http.Response response = await http.get(
apiUrl,
headers: {
HttpHeaders.authorizationHeader:
"563492ad6f91700001000001999da5bd71d04ece9af9ba1a03e8beaf"
},
);
print(apiUrl);
if (response.statusCode == 200) {
try {
final responseJson = jsonDecode(response.body);
wallpapersList = responseJson['photos'];
} catch (e) {
print(e);
}
} else
print(response.reasonPhrase);
}
@override
Widget build(BuildContext context) {
var tabindex = 0;
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: searchBar,
backgroundColor: myColor,
actions: [
IconButton(
icon: searchIcon,
onPressed: () {
setState(() {
if (this.searchIcon.icon == Icons.search) {
this.searchIcon = Icon(Icons.cancel);
this.searchBar = TextField(
textInputAction: TextInputAction.go,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search",
hintStyle: TextStyle(color: Colors.white)),
onSubmitted: (value) {
query = value;
initialize();
print(query);
},
);
} else {
this.searchIcon = Icon(Icons.search);
this.searchBar = Text("FotoApp");
}
});
},
color: Colors.white,
)
],
),
body: wallpapersList != null
? StaggeredGridView.countBuilder(
padding: const EdgeInsets.all(8.0),
crossAxisCount: 4,
itemBuilder: (BuildContext context, int index) {
String imgPath = wallpapersList[index]["src"]["large"];
return Card(
child: InkWell(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImagePath(imgPath))),
child: Hero(
tag: imgPath,
child: FadeInImage(
width: MediaQuery.of(context).size.width,
placeholder: AssetImage("assets/loading.gif"),
image: NetworkImage(imgPath),
fit: BoxFit.cover,
)),
),
);
},
staggeredTileBuilder: (index) =>
StaggeredTile.count(2, index.isEven ? 2 : 3),
itemCount: wallpapersList.length,
)
: Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.black,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.grey,
currentIndex: tabindex,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.image), title: Text("Wallpapers")),
BottomNavigationBarItem(
icon: Icon(Icons.view_list), title: Text("Categories")),
BottomNavigationBarItem(
icon: Icon(Icons.info), title: Text("About"))
],
),
backgroundColor: myColor2,
),
);
}
}
导入'dart:async';
导入“dart:convert”;
导入“dart:io”;
将“package:http/http.dart”导入为http;
导入“package:FotoApp/image.dart”;
进口“包装:颤振/材料.省道”;
导入“包:颤振交错网格视图/颤振交错网格视图.省道”;
进口“包装:颤振_下载器/颤振_下载器.dart”;
最终颜色myColor=颜色(0xff222f3e);
最终颜色myColor2=颜色(0xff2f3640);
void main()异步{
WidgetsFlutterBinding.ensureInitialized();
等待下载程序。初始化(调试:true);
runApp(
MyApp(),
);
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
静态字符串query=“墙纸”;
名单;
图标搜索图标=图标(Icons.search);
小部件搜索栏=文本(“FotoApp”);
@凌驾
void initState(){
super.initState();
初始化();
}
void initialize()异步{
阿皮乌尔变种=
"https://api.pexels.com/v1/search?query=“+query+”&per_page=500”;
http.Response-Response=等待http.get(
阿皮乌尔,
标题:{
HttpHeaders.authorizationHeader:
“563492AD6F91700001999DA5BD71D04ECE9AF9BA1A03E8BEAF”
},
);
打印(apirl);
如果(response.statusCode==200){
试一试{
final responseJson=jsonDecode(response.body);
墙纸列表=响应['photos'];
}捕获(e){
印刷品(e);
}
}否则
打印(应答.短语);
}
@凌驾
小部件构建(构建上下文){
var指数=0;
返回材料PP(
debugShowCheckedModeBanner:false,
家:脚手架(
appBar:appBar(
标题:搜索栏,
背景颜色:myColor,
行动:[
图标按钮(
图标:搜索图标,
已按下:(){
设置状态(){
if(this.searchIcon.icon==Icons.search){
this.searchIcon=图标(Icons.cancel);
this.searchBar=TextField(
textInputAction:textInputAction.go,
样式:TextStyle(颜色:Colors.white),
装饰:输入装饰(
边框:InputBorder.none,
hintText:“搜索”,
hintStyle:TextStyle(颜色:Colors.white)),
提交:(值){
查询=值;
初始化();
打印(查询);
},
);
}否则{
this.searchIcon=图标(Icons.search);
this.searchBar=文本(“FotoApp”);
}
});
},
颜色:颜色,白色,
)
],
),
正文:墙纸列表!=空
?StaggedGridView.countBuilder(
填充:常数边集全部(8.0),
交叉轴计数:4,
itemBuilder:(构建上下文,int索引){
字符串imgPath=wallperslist[index][“src”][“large”];
回程卡(
孩子:InkWell(
onTap:()=>Navigator.push(
上下文
材料路线(
生成器:(context)=>ImagePath(imgPath)),
孩子:英雄(
标签:imgPath,
孩子:法代尼玛(
宽度:MediaQuery.of(context).size.width,
占位符:AssetImage(“assets/loading.gif”),
图像:网络图像(imgPath),
适合:BoxFit.cover,
)),
),
);
},
交错文件生成器:(索引)=>
交错文件计数(2,索引为2:3),
itemCount:WallpersList.length,
)
:中心(
子对象:循环压缩机指示器(
背景颜色:Colors.white,
),
),
底部导航栏:底部导航栏(
背景颜色:Colors.black,
选择编辑颜色:Colors.black,
unselectedItemColor:Colors.grey,
currentIndex:tabindex,
类型:BottomNavigationBarType.Shift,
项目:[
底部导航气压计(
图标:图标(Icons.image),标题:文本(“壁纸”),
底部导航气压计(
图标:图标(图标.视图列表),标题:文本(“类别”),
底部导航气压计(
图标:图标(Icons.info),标题:文本(“关于”))
],
),
背景颜色:myColor2,
),
);
}
}
这是因为用户界面有一个状态,检索数据时必须更改状态。
应用程序不知道它必须重建自己。按下按钮时正在进行重建
更好的方法是使用FutureBuilder并将未来(即接收到FutureBuilder的数据)传递给FutureBuilder进行渲染
当数据尚未准备就绪时显示循环进度指示器,并在数据准备就绪时显示数据。查看FutureBuilder的isBusy属性以了解此信息。替换此:
试试看{
final responseJson=jsonDecode(response.body);
墙纸列表=响应['photos'];
}捕获(e){
印刷品(e);
}
为此:
试试看{
final responseJson=jsonDecode(response.body);
设置状态(){
墙纸列表=响应['photos'];
});
}捕获(e){
印刷品(e);
}
我对Flitter还很陌生,你能告诉我具体在哪里进行更改吗