0

我正在构建一个应用程序,该应用程序在屏幕上列出了多个正在运行的计时器,用户可以通过刷卡将其关闭。为了更新计时器,我每隔一秒设置一次状态。问题在于滑动操作并不总是响应。它动画但不关闭。

我已经模拟了一个快速演示来显示问题。如果您在计数器更新后准确滑动,它应该可以工作......如果您在计数器更新之前滑动,它不会。

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Timer test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // ignore: unused_field
  late Timer _everySecond;
  int counter = 0;

  @override
  void initState() {
    super.initState();
    // defines a timer
    _everySecond = Timer.periodic(const Duration(seconds: 1), (Timer t) async {
      setState(() {
        counter++;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Timer test'),
      ),
      body: Dismissible(
        key: UniqueKey(),
        onDismissed: (direction) => counter = 0,
        background: Container(color: Colors.black38),
        child: SizedBox(
          height: 120,
          child: Center(
            child: Text(counter.toString()),
          ),
        ),
      ),
    );
  }
}

有没有其他方法可以更好地处理这个问题?

4

1 回答 1

0

我认为您的问题与关闭卡时不使用 setState 有关。始终牢记在 setState 方法中执行状态更改逻辑,以通知 Flutter 状态更改,从而发出需要重绘屏幕的信号。下面的代码应该做你想做的事:

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Timer test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // ignore: unused_field
  late Timer _everySecond;
  int counter = 0;

  @override
  void initState() {
    super.initState();
    // defines a timer
    _everySecond = Timer.periodic(const Duration(seconds: 1), (Timer t) async {
      setState(() {
        counter++;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Timer test'),
      ),
      body: Dismissible(
        key: UniqueKey(),
        onDismissed: (direction) => setState({counter = 0;}),
        background: Container(color: Colors.black38),
        child: SizedBox(
          height: 120,
          child: Center(
            child: Text(counter.toString()),
          ),
        ),
      ),
    );
  }
}
于 2022-02-28T19:23:23.537 回答