GETX:如何正确使用 Future

时间:2021-07-18 08:54:07

标签: flutter

我试图通过 List.generate(1000, (i) => i) 来理解示例中的未来

当点击一个元素时 => 转到带有切换的详细信息屏幕以将该元素保存到本地

回到主界面,本地的元素会高亮显示

主屏幕的复选框将过滤是否显示本地元素

enter image description here

所以我想知道 如何处理切换的 Future 的正确方法,以便在单击它时 => 它仍然会很流畅,没有滞后或延迟,但之后的所有功能仍将执行之后

  Future<void> onToggle(bool value, int id) async {
    boolToggle.value = value;
    listToggle.remove(id);
    if (value) listToggle.add(id);
    // box.write('listToggle', listToggle); * does using worker ever instead make it smoother?
    onLoadPage();
  }

所以请为我编辑它以使代码更清晰,这是完整的代码:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

void main() async {
  await GetStorage.init();
  await Get.put(HomeController());
  runApp(GetMaterialApp(
    debugShowCheckedModeBanner: false,
    home: HomeView(),
  ));
}

class HomeController extends GetxController {
  var listRaw = List.generate(1000, (i) => i);
  final box = GetStorage();
  List<int> list;
  RxBool boolToggle = false.obs;
  RxBool isChecked = true.obs;
  RxList<int> listToggle = RxList<int>();

  @override
  void onInit() {
    super.onInit();
    List<int> a = GetStorage().read('listToggle')?.cast<int>() ?? [];
    listToggle = a.toList().obs;
    ever(listToggle, (value) => box.write('listToggle', value));
    onLoadPage();
  }

  Future<void> onToggle(bool value, int id) async {
    boolToggle.value = value;
    listToggle.remove(id);
    if (value) listToggle.add(id);
    // box.write('listToggle', listToggle); * does using worker ever instead make it smoother?
    onLoadPage();
  }

  onLoadPage({bool value}) {
    list = List.from(listRaw);
    isChecked.value = value == null ? isChecked.value : value;
    if (isChecked.value) list.retainWhere((e) => !listToggle.contains(e));
    update();
  }
}

class HomeView extends GetView<HomeController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          actions: [
            Obx(
              () => Checkbox(
                value: controller.isChecked.value,
                onChanged: (value) => controller.onLoadPage(value: value),
              ),
            )
          ],
        ),
        body: GetBuilder<HomeController>(builder: (_) {
          return ListView.builder(
              itemCount: controller.list.length,
              itemBuilder: (context, index) {
                final i = controller.list[index];
                return Card(
                    color: controller.listToggle.contains(i) ? Colors.blue.withOpacity(0.2) : null,
                    child: GestureDetector(
                      onTap: () => Get.to(() => DetailView(i: i)),
                      child: Text(i.toString()),
                    ));
              });
        }));
  }
}

class DetailView extends GetView<HomeController> {
  final int i;
  const DetailView({this.i});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i.toString()),
        actions: [
          Obx(() => Switch(
                value: controller.listToggle.contains(i),
                onChanged: (value) => controller.onToggle(value, i),
              )),
        ],
      ),
    );
  }
}

0 个答案:

没有答案