我有兩個 Textform 欄位Billing Address,Shipping Address當用戶在 textform 欄位中輸入帳單地址時,我想將這些資料復制到我的送貨地址欄位checkBox。如果用戶Shipping和用戶Billing Address相同,我不希望用戶再次將資料放入送貨地址欄位。
這是我的課
class Shipping_Address_Page extends StatefulWidget {
const Shipping_Address_Page({Key? key}) : super(key: key);
@override
State<Shipping_Address_Page> createState() => _Shipping_Address_PageState();
}
class _Shipping_Address_PageState extends State<Shipping_Address_Page> {
bool sameAddress = false;
//===================================billing controller===============================
late TextEditingController UserBilling_Name_Controller =TextEditingController(text: UserBillingNAme);
late TextEditingController UserBilling_Email_Controller =TextEditingController(text: UserBillingEmail);
//===================================shipping===============================
late TextEditingController UserShipping_NAme_Controller =TextEditingController(text: UserShippingNAme);
late TextEditingController UserShipping_Email_Controller =TextEditingController(text: UserShippingEmail);
textform 欄位的 UI 代碼
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
FutureBuilder(
future: PredifinedAddressModel_Api(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(
child:
CupertinoActivityIndicator());
}
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
//=================================Billing address=============================
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFE4D8DC)),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(20),
vertical: getProportionateScreenHeight(30)),
child: Column(
children: [
Container(
decoration: BoxDecoration(
color: Color(0xFFf0f0f0),
borderRadius: BorderRadius.circular(15),
),
margin: EdgeInsets.all(10),
child: TextFormField(
textCapitalization: TextCapitalization.sentences,
controller: UserBilling_Name_Controller,//===Controller
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(
horizontal:
getProportionateScreenWidth(20),
vertical:
getProportionateScreenWidth(
15)),
border: InputBorder.none,
focusedBorder: InputBorder.none,
labelText: "Name",
enabledBorder: InputBorder.none,
hintText: "Your name",
hintStyle: TextStyle(
color:
Colors.black.withOpacity(0.4)),
prefixIcon:
Icon(Icons.account_circle,size: 13.0)),
),
),
//===================================Shipping===============================
SizedBox(height: getProportionateScreenHeight(20),),
Padding(
padding: EdgeInsets.only(left: 8.0, bottom: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: getProportionateScreenHeight(20),
),
Text(
"Shipping address",
style: TextStyle(
fontSize: getProportionateScreenHeight(30),
color: Colors.black,
fontFamily: 'Gilroy',
fontWeight: FontWeight.w700),
),
],
),
),
SizedBox(height: getProportionateScreenHeight(20),),
//=================================check box=====================================
Row(
children: [
Checkbox(
value: sameAddress, //false
activeColor: Colors.green,
onChanged: (value) {
setState(() {
sameAddress = value!;
});
},
),
Text("Ship to Same address?"),
],
),
//=====================================================================================
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFC8E3D4)),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(20),
vertical: getProportionateScreenHeight(30)),
child: Column(
children: [
Container(
decoration: BoxDecoration(
color: Color(0xFFf0f0f0),
borderRadius: BorderRadius.circular(15),
),
margin: EdgeInsets.all(10),
child: TextFormField(
textCapitalization: TextCapitalization.sentences,
controller: UserShipping_NAme_Controller,//===Controller
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(
horizontal:
getProportionateScreenWidth(20),
vertical:
getProportionateScreenWidth(
15)),
border: InputBorder.none,
focusedBorder: InputBorder.none,
labelText: "Name",
enabledBorder: InputBorder.none,
hintText: "Your name",
hintStyle: TextStyle(
color:
Colors.black.withOpacity(0.4)),
prefixIcon:
Icon(Icons.account_circle,size: 13.0)),
),
),
uj5u.com熱心網友回復:
Row(
children: [
Checkbox(
value: sameAddress, //false
activeColor: Colors.green,
onChanged: (value) {
setState(() {
UserShipping_NAme_Controller.text = UserBilling_Name_Controller.text;
UserShipping_Email_Controller.text = UserBilling_Email_Controller.text;
});
},
),
Text("Ship to Same address?"),
],
),
用此代碼替換包含復選框的行。
uj5u.com熱心網友回復:
您可以controller在需要時顯式設定setState
For 例如的值。
billingTextController.text = shippingTextController.text;
uj5u.com熱心網友回復:
您可以添加 2 個控制器并根據復選框值將這兩個控制器等同起來
late TextEditingController UserBilling_Address_Controller =TextEditingController(text: UserBillingAddress);
late TextEditingController UserShipping_Address_Controller =TextEditingController(text: UserShippingAddress);
復選框 onChange
UserShipping_Address_Controller = value ? UserBilling_Address_Controller.text : ""
uj5u.com熱心網友回復:
試試下面的代碼希望對你有幫助。
創建你的TextEditingController()變數
final shippingController = TextEditingController();
final billingController = TextEditingController();
復選框的 boolaen 變數:
bool isChecked = false;
你的小部件:
Column(
children: [
TextFormField(
controller: shippingController,
decoration: InputDecoration(
labelText: 'shipping address',
border: OutlineInputBorder(),
),
),
TextFormField(
controller: billingController,
decoration: InputDecoration(
labelText: 'billing address',
border: OutlineInputBorder(),
),
),
SizedBox(
height: 50,
),
Row(
children: [
Checkbox(
value: isChecked, //false
activeColor: Colors.green,
onChanged: (value) {
setState(() {
billingController.text =
shippingController.text;
});
},
),
Text("Ship to Same address?"),
],
),
],
),
結果畫面>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/402060.html
上一篇:EFCore:嵌套集合(Func<>)作為變數的過濾條件
下一篇:串列視圖中的容器占用了更多寬度
