我的應用程式中有一個 SwitchListTile,它根據其值確定應寫入另一個按鈕的文本。一切正常,除了第一次打開應用程式時,按鈕上的初始文本將什么都不是,就像第一次宣告一樣,直到我第一次切換 SwitchListTile。所以我想知道如何讓文本從你進入這個頁面的那一刻開始出現。
這是我的代碼:
static String buttonText='';
SwitchListTile(
title: const Text('Enable Bluetooth'),
value: _bluetoothState.isEnabled,
onChanged: (bool value) {
// Do the request and update with the true value then
future() async {
// async lambda seems to not working
if (value) {
await FlutterBluetoothSerial.instance.requestEnable();
setState(() {buttonText = "Press to start collection" ; });
} else {
await FlutterBluetoothSerial.instance.requestDisable();
setState(() {buttonText = "Please enable bluetooth in your device" ; });
}
}
future().then((_) {
setState(() {});
});
},
),
.......................
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red,
padding: const EdgeInsets.symmetric(horizontal: 30, vertical:40),
textStyle:
const TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
onPressed: () {
if(_bluetoothState.isEnabled){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const BluetoothConnectionTask()),
);
}
},
child: Text(
buttonText,
style: TextStyle(fontSize: 28,color: Colors.black, letterSpacing: 2.0),
),
),
uj5u.com熱心網友回復:
請如下更新提升按鈕中設定的文本小部件。
Text(
_bluetoothState.isEnabled ? "Press to start collection" : "Please enable bluetooth in your device",
style: TextStyle(fontSize: 28,color: Colors.black, letterSpacing: 2.0),
)
并洗掉
setState(() {buttonText = "Press to start collection" ; });
setState(() {buttonText = "Please enable bluetooth in your device" ; });
來自 onChanged 方法。
它從第一次開始就不起作用,因為當您與 SwitchListTile 互動時,會呼叫 onChanged 方法。
如果仍然無法正常作業,請讓我知道該問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/492756.html
