我創建了一個帶有 toogle 按鈕的兩個螢屏,費用螢屏和收入螢屏,用于將費用螢屏切換到收入螢屏,反之亦然。
這里我有一個金額的文本欄位..它的值在轉移到另一個螢屏時不應該改變......
(我的意思是如果將支出螢屏更改為收入螢屏,文本欄位中的金額不應更改...
這是我的主檔案
class _MyAppState extends State<MyApp> {
bool isexpense=true;
void toogleit()
{
setState(() {
isexpense=!isexpense;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: isexpense==true? ExpenseScreen(ontap: toogleit,txtvalue: '',):IncomeScreen(ontap: toogleit,txtvalue: '',)
);
}
}
這是費用螢屏檔案……不包括收入螢屏,因為兩者幾乎相同……
(我可以制作一個 CustomScreenWidget,但不想制作它......我被賦予了在沒有 customewidget 的情況下解決的任務)
這是費用螢屏代碼
class ExpenseScreen extends StatefulWidget {
final VoidCallback ontap;
String txtvalue;
ExpenseScreen({Key? key, required this.ontap,required this.txtvalue}) : super(key: key);
@override
State<ExpenseScreen> createState() => _ExpenseScreenState();
}
class _ExpenseScreenState extends State<ExpenseScreen> {
TextEditingController txtcontroller = TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
txtcontroller.text=widget.txtvalue;
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
txtcontroller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Container(
height: 300,
decoration: kboxdecoration1,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IntrinsicWidth(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: TextField(
onChanged: (x) {
setState(() {
});
},
controller: txtcontroller,
keyboardType: TextInputType.number,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 30,
),
decoration: kinputdecoration1.copyWith(
hintText:
txtcontroller.text == '' ? 'Amount' : null),
),
),
),
SizedBox(
height: 10,
),
ElevatedButton(
style: kelevetedbutton1,
onPressed: () {},
child: Text('Save Expense')),
SizedBox(
height: 10,
),
TextButton(
onPressed: widget.ontap, child: Text('Jump To Income')),
],
),
),
),
),
),
);
}
}
uj5u.com熱心網友回復:
您需要更新您的回呼以txtvalue
像這樣更新:
class ExpenseScreen extends StatefulWidget {
final Function(String) ontap;
String txtvalue;
ExpenseScreen({Key? key, required this.ontap,required this.txtvalue}) : super(key: key);
@override
State<ExpenseScreen> createState() => _ExpenseScreenState();
}
然后在你的 onPressed 上這樣做:
TextButton(
onPressed: (){
widget.ontap(txtcontroller.text);
}, child: Text('Jump To Income'),
),
然后在您的MainScreen
第一個變數中定義一個變數并執行以下操作:
String txtvalue = '';
void toogleit(String value)
{
setState(() {
txtvalue = value;
isexpense=!isexpense;
});
}
并在您的中更改此設定mainscreen
:
home: isexpense==true? ExpenseScreen(ontap: toogleit,txtvalue: txtvalue,):IncomeScreen(ontap: toogleit,txtvalue: txtvalue,)
uj5u.com熱心網友回復:
有兩種方法,
使用建構式,正如@eamirho3ein 所指出的那樣,并在訪問第二個螢屏時傳遞它
創建另一個 Dart 檔案并將值保存為靜態值,例如,創建一個檔案 ApplicationData 并將值存盤為靜態值。確保無論何時發生螢屏變化,然后將當前金額值存盤在靜態欄位中
ApplicationData.amount
。
隨時隨地訪問它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/535422.html
標籤:扑