這是我的代碼,
我是用TextButton更新訂單的,但是每次修改下拉項后,會自動呼叫onPress并自動呼叫updateOrder函式
import 'package:admin/constants/Constants.dart';
import 'package:admin/model/order_model.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
class DetailsScreen extends StatefulWidget {
const DetailsScreen({Key? key}) : super(key: key);
@override
State<DetailsScreen> createState() => _DetailsScreenState();
}
class _DetailsScreenState extends State<DetailsScreen> {
List<String> _dropDownQuantities = [Pending, Confirmed, Rejected, Success];
late OrderModel order;
late String selectedStatus = order.status;
@override
Widget build(BuildContext context) {
order = ModalRoute.of(context)?.settings.arguments as OrderModel;
return Scaffold(
appBar: AppBar(
actions: [],
title: Text("Order Details"),
),
body: Column(children: [
Text(order.id),
DropdownButtonHideUnderline(
child: DropdownButton2<String>(
value: selectedStatus,
items: _dropDownQuantities
.map((e) => DropdownMenuItem<String>(
child: Text(e),
value: e,
))
.toList(),
onChanged: (value) {
setState(() {
selectedStatus = value;
});
},
)),
TextButton(onPressed: updateOrder(order, selectedStatus), child: Text("Confirm")),
]));
}
}
updateOrder(OrderModel order, String selected) {
print("I am executed");
}
所以每當我改變下拉選單時,
I am executed列印在控制臺中。
編輯:
但是當我將容器與 InkWell 一起使用時,它作業正常。為什么不使用 TextButton ?
uj5u.com熱心網友回復:
您在構建時直接呼叫該方法,您可以創建一個行內匿名函式來處理此問題。
TextButton(
onPressed: ()=> updateOrder(order, selectedStatus),
child: Text("Confirm")),
onPressed當按鈕被點擊或以其他方式激活時呼叫。
雖然我們onPressed:method()在每次構建時都使用 call ,但在 dropDown 上onChanged我們使用setState,它會重建 UI 并onPressed:method()再次呼叫。
我們在這里需要的是傳遞一個函式(VoidCallback),當我們點擊按鈕時它會觸發。我們提供它,
onPressed:(){
myMethod();
}
更多關于TextButton.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477653.html
