我怎樣才能將螢屏分成兩個不同的部分。我正在學習過濾器,我做了這個代碼 我想從服務器上添加圖片 當我第一次放入1號或2號圖片時,沒有問題。 但當選擇第二張圖片時,第一張圖片被洗掉了。我想是因為它重繪 了頁面(重建)。 我怎樣才能把兩張圖片放在一個螢屏上?最好的方法是什么?
謝謝你
import 'dart:convert'。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lifeetb/constants/my_colors.dart';
import 'package:http/http.dart' as http;
import 'package:share_preferences/shared_preferences.dart'。
class BeforeAfter2 extends StatefulWidget{
const BeforeAfter2({
key? key。
this.imageSelected1,
this.imageSelected2,
}) : super(key: 關鍵)。
finalimageSelected1;
final imageSelected2;
@override
_BeforeAfter2State createState() => _BeforeAfter2State()。
}
//將其改為Cata。
var imageSelected1;
var imageSelected2;
var imageBody = true;
var id;
var username;
var email;
getPref() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
username = preferences.getString("username")。
id = preferences.getString("id")。
email = preferences.getString("email")。
if ( id == null) {
print(" empty")。
}
}
Future getData() async {
getPref()。
if ( id == null) {
print(" empty 2")。
}
var url = "http://10.0.2.2/641984.php"/span>;
var data = {"user_id"/span>: "$id"}。
var response = await http.post(Uri.parse(url), body: data) 。
var responsebody = jsonDecode(response.body)。
return responsebody。
}
class _BeforeAfter2State extends State < BeforeAfter2> {
@override>
Widget build(BuildContext context) {
return SafeArea(
孩子。Scaffold(
body: 列(
兒童。[
擴展的(
靈活。1,
孩子。SizedBox(
高度。double.infinity,
width: double.infinity,
child: widget.imageSelected1 == null ?
? addImage(true,true)
: showCata()
),
),
擴展的(
彎曲。1,
孩子。SizedBox(
高度。double.infinity,
width: double.infinity,
child: widget.imageSelected2 == null ?
? addImage(false,true)
: showCata1()
),
),
],
),
));
}
}
addImage(addText, goToAdd) {
return Column(
兒童。[
柱子(
mainAxisAlignment: MainAxisAlignment.center,
兒童。[
addText ? Text("Add 1 Photo") : Text("Add 2 Photo") 。
SizedBox(高度:26)。
容器(
對齊。Alignment.center,
孩子。FloatingActionButton(
heroTag: null。
孩子。圖示(
Icons.add,
color: white,
大小。45。
),
onPressed: () {
去添加
? showCata()
: showCata1();
},
)),
],
),
],
);
}
showCata(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (! snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
兒童。[
Text('Loading...')。
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15)。
孩子。Text("You don't Have any Photo")
);
}
return GridView.buildinger(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder。(context, i) {
return Padding(
padding: const EdgeInsets.all(4.0)。
孩子。InkWell()
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected1: snapshot.data[i]["post_image"] 。
);
}));
},
孩子。容器(
margin: EdgeInsets.only(top: 10, left: 10)。)
裝飾。BoxDecoration(
邊界。border.all(
color: Colors.deepPurple.shade500,
width: 2)。)
borderRadius:
borderRadius.all(Radius.round(35))。
影像。DecorationImage()
適合。BoxFit.cover,
影像。網路影像(
"http://10.0.2.2/upload/"
snapshot.data[i]["post_image"])。)
)),
),
),
);
},
);
},
);
}
showCata1(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (! snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
兒童。[
Text('Loading...')。
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15)。
孩子。Text("You don't Have any Photo")
);
}
return GridView.buildinger(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder。(context, i) {
return Padding(
padding: const EdgeInsets.all(4.0)。
孩子。InkWell()
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected2: snapshot.data[i]["post_image"] 。
);
}));
},
孩子。容器(
margin: EdgeInsets.only(top: 10, left: 10)。)
裝飾。BoxDecoration(
邊界。border.all(
color: Colors.deepPurple.shade500,
width: 2)。)
borderRadius:
borderRadius.all(Radius.round(35))。
影像。DecorationImage()
適合。BoxFit.cover,
影像。網路影像(
"http://10.0.2.2/upload/"
snapshot.data[i]["post_image"])。)
)),
),
),
);
},
);
},
);
}
uj5u.com熱心網友回復:
洗掉SizedBox()s,用child改變SafeArea
uj5u.com熱心網友回復:
class _BeforeAfter2State extends State< BeforeAfter2> {
@override>
Widget build(BuildContext context) {
return SafeArea(
孩子。Scaffold(
body: 列(
兒童。[
擴展的(
靈活。1,
child: widget.imageSelected1 == null ?
? addImage(true,true)
: showCata()
),
展開(
彎曲。1,
child: widget.imageSelected2 == null ?
? addImage(false,true)
: showCata1()
),
],
),
));
}
}
addImage(addText, goToAdd) {
return Column(
兒童。[
柱子(
mainAxisAlignment: MainAxisAlignment.center,
兒童。[
addText ? Text("Add 1 Photo") : Text("Add 2 Photo") 。
SizedBox(高度:26)。
容器(
對齊。Alignment.center,
孩子。FloatingActionButton(
heroTag: null。
孩子。圖示(
Icons.add,
color: white,
大小。45。
),
onPressed: () {
去添加
? showCata()
: showCata1();
},
)),
],
),
],
);
}
showCata(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (! snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
兒童。[
Text('Loading...')。
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15)。
孩子。Text("You don't Have any Photo")
);
}
return GridView.buildinger(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder。(context, i) {
return Padding(
padding: const EdgeInsets.all(4.0)。
孩子。InkWell()
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected1: snapshot.data[i]["post_image"] 。
);
}));
},
孩子。容器(
margin: EdgeInsets.only(top: 10, left: 10)。)
裝飾。BoxDecoration(
邊界。border.all(
color: Colors.deepPurple.shade500,
width: 2)。)
borderRadius:
borderRadius.all(Radius.round(35))。
影像。DecorationImage()
適合。BoxFit.cover,
影像。網路影像(
"http://10.0.2.2/upload/"
snapshot.data[i]["post_image"])。)
)),
),
),
);
},
);
},
);
}
showCata1(){
return FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (! snapshot.hasData) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
兒童。[
Text('Loading...')。
Center(child: CircularProgressIndicator())
],
);
} else if (snapshot.data.isEmpty) {
return Padding(
padding: EdgeInsets.all(15)。
孩子。Text("You don't Have any Photo")
);
}
return GridView.buildinger(
// shrinkWrap: true,
itemCount: snapshot.data.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// crossAxisSpacing: 10
),
itemBuilder。(context, i) {
return Padding(
padding: const EdgeInsets.all(4.0)。
孩子。InkWell()
onTap: () {
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BeforeAfter2(
imageSelected2: snapshot.data[i]["post_image"] 。
);
}));
},
孩子。容器(
margin: EdgeInsets.only(top: 10, left: 10)。)
裝飾。BoxDecoration(
邊界。border.all(
color: Colors.deepPurple.shade500,
width: 2)。)
borderRadius:
borderRadius.all(Radius.round(35))。
影像。DecorationImage()
適合。BoxFit.cover,
影像。網路影像(
"http://10.0.2.2/upload/"
snapshot.data[i]["post_image"])。)
)),
),
),
);
},
);
},
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326617.html
標籤:
