[英]How to align DropdownButton next to a TextField in Flutter?
[英]Flutter : Trying pass a variable to next screen based on DropDownButton input
我正在嘗試根據用戶下拉輸入降低產品(智能手機)價格並將價格傳遞到下一個屏幕。 我的代碼沒有錯誤並且運行完美,但問題是,它僅基於一個 switch 語句(為下拉值全局選擇的初始語句)降低了價格,因為setState
方法僅適用於更改屏幕上顯示的下拉值,而不是在開關中包含 function 的語句。 有人可以幫我解決這個問題。 我嘗試了很多東西,但沒有任何幫助。 下面是我的代碼(它很長,所以我在這里跳過了小部件構建部件和設計部件)。
值已被定義為這樣
double price = 18000;
String bdropDownValue = 'Less than 6 hours'; //initialvalue
final List<String> bItems = [
'Less than 6 hours',
'Less than 12 hours',
'Less than 24 hours'],
下拉按鈕表單字段
DropdownButtonFormField(value: bdropDownValue,
items: bItems.map((bItems) {
return DropdownMenuItem(
value: bItems,
child: Text(bItems),
);
}).toList(),
onChanged: (String? value) {
setState(() {
bdropDownValue = value!;
});
},
),
/// button used to calculate next
TextButton(
onPressed: () {
calculator(bdropDownValue);
},
child: Text(
"confirm",
),
我正在使用這種方法來計算
void calculator(String bdropDownValue) {
switch (bdropDownValue) {
case 'Less than 6 hours':
price = price - 3000;
break;
case 'Less than 12 hours':
price = price - 1500;
break;
case 'Less than 24 hours':
price = price - 1000;
break;
}
並在計算器方法中使用這樣的Navigator
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => priceScreen(
price: price,
)));
}
}
請檢查我在下面添加的示例,它工作得很好。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MainHomePage());
}
}
class MainHomePage extends StatefulWidget {
MainHomePage({Key key}) : super(key: key);
@override
_MainHomePageState createState() => _MainHomePageState();
}
class _MainHomePageState extends State<MainHomePage> {
double price = 18000;
String bdropDownValue = 'Less than 6 hours'; //initialvalue
final List<String> bItems = [
'Less than 6 hours',
'Less than 12 hours',
'Less than 24 hours'
];
void calculator(String bdropDownValue) {
var actualPrice = price;
switch (bdropDownValue) {
case 'Less than 6 hours':
actualPrice = actualPrice - 3000;
break;
case 'Less than 12 hours':
actualPrice = actualPrice - 1500;
break;
case 'Less than 24 hours':
actualPrice = actualPrice - 1000;
break;
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PriceScreen(
price: actualPrice,
)));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
DropdownButtonFormField(
value: bdropDownValue,
items: bItems.map((bItems) {
return DropdownMenuItem(
value: bItems,
child: Text(bItems),
);
}).toList(),
onChanged: (String value) {
setState(() {
bdropDownValue = value;
});
},
),
TextButton(
onPressed: () {
calculator(bdropDownValue);
},
child: Text(
"confirm",
),
),
],
),
),
);
}
}
class PriceScreen extends StatelessWidget {
final double price;
const PriceScreen({Key key, this.price}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text(price.toString()),
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.