Introduce Switch.padding (#149884)

fixes [Switch has some padding that leads to uncentered UI](https://github.com/flutter/flutter/issues/148498)

### Code sample

<details>
<summary>expand to view the code sample</summary> 

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

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ColoredBox(
                color: Colors.amber,
                child: Switch(
                  padding: EdgeInsets.zero,
                  value: true,
                  materialTapTargetSize: MaterialTapTargetSize.padded,
                  onChanged: (bool value) {},
                ),
              ),
              const SizedBox(height: 16),
              ColoredBox(
                color: Colors.amber,
                child: Switch(
                  value: true,
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  onChanged: (bool value) {},
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
```

</details>

### Default Switch size

<img width="476" alt="Screenshot 2024-07-11 at 13 25 05" src="https://github.com/flutter/flutter/assets/48603081/f9f3f6c6-443d-4bd5-81d4-5e314554b032">

### Update Switch size using the new `Switch.padding` to address  [Switch has some padding that leads to uncentered UI](https://github.com/flutter/flutter/issues/148498)

<img width="476" alt="Screenshot 2024-07-11 at 13 24 40" src="https://github.com/flutter/flutter/assets/48603081/aea0717b-e852-4b8d-b703-c8c4999d4863">
This commit is contained in:
Taha Tesser
2024-07-16 23:25:09 +03:00
committed by GitHub
parent 22a5c6cb0a
commit e1cd7b11f6
5 changed files with 140 additions and 18 deletions

View File

@@ -137,6 +137,9 @@ class _${blockName}DefaultsM3 extends SwitchThemeData {
@override
double get splashRadius => ${getToken('md.comp.switch.state-layer.size')} / 2;
@override
EdgeInsetsGeometry? get padding => const EdgeInsets.symmetric(horizontal: 4);
}
class _SwitchConfigM3 with _SwitchConfig {
@@ -192,13 +195,13 @@ class _SwitchConfigM3 with _SwitchConfig {
double get pressedThumbRadius => ${getToken('md.comp.switch.pressed.handle.width')} / 2;
@override
double get switchHeight => _kSwitchMinSize + 8.0;
double get switchHeight => switchMinSize.height + 8.0;
@override
double get switchHeightCollapsed => _kSwitchMinSize;
double get switchHeightCollapsed => switchMinSize.height;
@override
double get switchWidth => trackWidth - 2 * (trackHeight / 2.0) + _kSwitchMinSize;
double get switchWidth => 52.0;
@override
double get thumbRadiusWithIcon => ${getToken('md.comp.switch.with-icon.handle.width')} / 2;
@@ -223,6 +226,9 @@ class _SwitchConfigM3 with _SwitchConfig {
// Hand coded default based on the animation specs.
@override
double? get thumbOffset => null;
@override
Size get switchMinSize => const Size(kMinInteractiveDimension, kMinInteractiveDimension - 8.0);
}
''';