大家好,我有 2 個 dart 檔案,我想使用file? image寫在檔案 offerform.dart 中的內容并將其匯入 Page2.dart 但我無法訪問它,你能告訴我我的代碼有什么問題嗎?非常感謝你
offerForm.dart
import 'package:firebase_auth/offersP.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
//import 'offersdetailsP.dart';
import 'package:image_picker/image_picker.dart';
class offerform extends StatelessWidget {
const offerform({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('????? ??? ????'),
),
body: const Center(
child: MyStatefulWidget(),
),
);
}
}
//enum offer_type { land,house,apartment }
//enum offer_method { sell,rent }
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});
@override
State<MyStatefulWidget> createState() => MyStatefulWidgetState();
}
class MyStatefulWidgetState extends State<MyStatefulWidget> {
String? offer_type;
String? offer_method;
final imageUrl = TextEditingController();
String sell = '???';
String rent = '????';
String land = '???';
String house = '???';
String apartment = '???';
File? image;
Future uploadImage() async{
try {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if(image == null) return;
final imageTemp = File(image.path);
setState(() => this.image = imageTemp ) ;
} on PlatformException catch(e) {
print('faild to upload $e');
}
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(padding: EdgeInsets.all(8.0)),
Text('?????', style: TextStyle(
fontSize: 18
),),
Divider(),
ListTile(
title: const Text('???'),
leading: Radio(
value: '???',
groupValue: offer_type,
onChanged: (value) {
setState(() {
offer_type = value;
});
},
),
),
ListTile(
title: const Text('???'),
leading: Radio(
value: '???',
groupValue: offer_type,
onChanged: (value) {
setState(() {
offer_type = value;
});
},
),
),
ListTile(
title: const Text('???'),
leading: Radio(
value: '???',
groupValue: offer_type,
onChanged: (value) {
setState(() {
offer_type = value;
});
},
),
),
Column(
children: <Widget>[
Text('???????', style: TextStyle(
fontSize: 18
),),
Divider(),
ListTile(
title: const Text('?????'),
leading : Radio(
value: '?????',
groupValue: offer_method,
onChanged: (value) {
setState(() {
offer_method = value;
});
},
),
),
ListTile(
title: const Text('??????'),
leading: Radio(
value: '??????',
groupValue: offer_method,
onChanged: (value) {
setState(() {
offer_method = value;
});
},
),
),
IconButton(
onPressed: uploadImage,
icon: Icon(Icons.add),
),
ElevatedButton(
onPressed: () {
offers_list.add(Offers(title: offer_type! ' ' offer_method! , describtion: "describtion", price: 190,image:imageUrl.text, availability: true, location: "location"));
print(offer_type! offer_method!);
print(imageUrl);
}, child: null,
),
],
)
],
);
}
}
這是我嘗試在其上匯入第一個檔案的第二個檔案
Page2.dart
import 'package:flutter/material.dart';
import 'offersP.dart';
//import 'offerForm.dart';
class offersPage extends StatelessWidget {
const offersPage({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: ListView.builder(itemCount : offers_list.length ,
itemBuilder: (context, index){
Offers offer = offers_list[index];
return Card(
child : Column (
children: <Widget>[
ListTile(
title: Text(offer.title.toString()),
subtitle: Text(offer.describtion.toString()),
leading: Image.network(),
onTap: (){
Navigator.push(context,
MaterialPageRoute(
builder: (context) => offersdetail(offer)));
},
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Icon(Icons.price_check),
Text(offer.price.toString()),
SizedBox(
width: 150.0,
),
Icon(Icons.location_on),
Text(offer.location.toString())
],
)
]),
);
}),
);
}
} ```
uj5u.com熱心網友回復:
如果你試試這個行嗎?
import offerform.dart as offerform
并在頁面上使用影像:
offerform.image
uj5u.com熱心網友回復:
您無法直接訪問它的原因是它在類內部,您必須首先創建該類的實體然后才能訪問它
但在這種情況下,這不是很有趣的方式。您應該使用狀態管理來擁有共享變數和方法,并能夠相應地更新您的 UI
您也可以使用靜態類將影像和下載影像的方法放入其中,但您無法選擇更新 UI
uj5u.com熱心網友回復:
你可以這樣使用它:
上級:
typedef void PathSelected(File? file);
但可能不適用于您的情況,因為沒有相關的offersPage課程MyStatefulWidget
我認為這個代碼想法更好,現在試試這個代碼:
class test{
static SharedPreferences? prefs;
Future<File?> uploadImage() async{
try {
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if(image == null) return;
setPath(image.path);
return File(image.path);
} on PlatformException catch(e) {
print('faild to upload $e');
}
}
static Future<void> setPath(String? path) async {
prefs = await SharedPreferences.getInstance();
return prefs!.setString('path', path);
}
static Future<String?> getPath() async{
prefs = await SharedPreferences.getInstance();
return prefs!.getString('path');
}
}
File(await test.getPath())并在類中設定函式offersPage
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/512885.html
標籤:扑镖
