Skip to content

Material switch toggle animation absent when disabling the switch in the same rebuild #61247

@ds84182

Description

@ds84182

Minimal reproduction:

import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(),
      body: Center(child: TestSwitch()),
    ),
  ));
}

class TestSwitch extends StatefulWidget {
  State<TestSwitch> createState() => _TestSwitchState();
}

class _TestSwitchState extends State<TestSwitch> {
  static const kDisableSwitchOnChanged = true; // Change me!

  bool value = true;
  bool enabled = true;

  Widget build(BuildContext context) {
    return Switch(
      value: value,
      onChanged: enabled ? _onChanged : null,
    );
  }

  void _onChanged(bool value) {
    setState(() {
      this.value = value;
      if (kDisableSwitchOnChanged) {
        enabled = false;
        Timer(Duration(seconds: 1), () {
          if (mounted) {
            setState(() {
              enabled = true;
            });
          }
        });
      }
    });
  }
}

If kDisableSwitchOnChanged is true, the switch toggle animation does not happen. It instead snaps to the end position.

For comparison, Android settings screen handles this correctly, and Cupertino switch handles it correctly.

cc @LongCatIsLooong

Metadata

Metadata

Labels

a: fidelityMatching the OEM platforms betterf: material designflutter/packages/flutter/material repository.found in release: 1.21Found to occur in 1.21frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions