如何从flutter中的toggle按钮获取api



我创建了一个切换按钮,我把代码放在下面:

Widget buildHeader({
required Widget child,
required String text,
}) =>
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
text,
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
),
const SizedBox(height: 3),
child,
],
);
Widget buildSwitch() => Transform.scale(
scale: 2,
child: Switch.adaptive(
activeColor: Colors.blueAccent,
activeTrackColor: Colors.blue.withOpacity(0.4),
splashRadius: 50,
value: value,
onChanged: (value) => setState(() => this.value = value),
),
);

CCD_ 1变量为:CCD_ 2

这很好,但我希望这个api/api/v1/main/enable/{userId}在上切换时运行

这个api/api/v1/main/disable/{userId}在切换关闭时运行,我该怎么做?

这两个api是PUT,我已经为GET api编写了代码,它运行良好,但我不知道如何使用这两个PUT api 的切换

我为get编写了如下代码,它运行得很好

print('bye');
String token = await getToken();
var response = await http.get(
Uri.parse('/api/v1/main/active/get'),
headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
});
if (response.statusCode == 200) {
print('ok');
} else {
print("no");
}
}

如果成功,您必须调用API方法并更改值:

Widget buildSwitch() => Transform.scale(
scale: 2,
child: Switch.adaptive(
activeColor: Colors.blueAccent,
activeTrackColor: Colors.blue.withOpacity(0.4),
splashRadius: 50,
value: value,
onChanged: (value) async {
await apiCallMethod().then((success) {
if (success) {
setState(() => this.value = value);
}
});
}
),
);
import 'dart:convert';
import 'package:http/http.dart' as http;
class ApiHelper{
static String baseUrl = "https://randomuser.me/api/?results=20";
static Future<List<dynamic>> getUserList(String endPoint) async {
var response = await http.get(Uri.parse(baseUrl+endPoint));
return jsonDecode(response.body)['results'];
}

}

您可以使用我的软件包api_toggle来帮助在重复单击时不多次调用api

最新更新