Skip to content

Switch,Radio, Checkbox don't inherit local theme properties #97691

@SuperPenguin

Description

@SuperPenguin

Steps to Reproduce

Current Switch right now doesn't use theme from nearest ancestor SwitchTheme widget, instead it use the theme from ThemeData.switchTheme anyway.

  1. Run code below
  2. See the switch thumb color

Expected results: 2nd switch thumb color should be green

Actual results: 2nd switch thumb color still red

Code sample
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const App());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Switch demo',
      theme: ThemeData(
        brightness: Brightness.light,
        switchTheme: SwitchThemeData(
          thumbColor: MaterialStateProperty.all(Colors.red),
        ),
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => const HomePage(),
      },
    );
  }
}

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

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _switch1 = false;
  bool _switch2 = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Switch demo'),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Switch(
              value: _switch1,
              onChanged: (value) {
                setState(() {
                  _switch1 = value;
                });
              },
            ),
            SwitchTheme(
              data: SwitchTheme.of(context).copyWith(
                thumbColor: MaterialStateProperty.all(Colors.green),
              ),
              child: Switch(
                value: _switch2,
                onChanged: (value) {
                  setState(() {
                    _switch2 = value;
                  });
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Logs
[√] Flutter (Channel master, 2.10.0-1.0.pre.402, on Microsoft Windows [Version 10.0.22000.434], locale en-001)
    • Flutter version 2.10.0-1.0.pre.402 at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ac52901c14 (58 minutes ago), 2022-02-02 19:28:25 -0800
    • Engine revision baadc162a9
    • Dart version 2.17.0 (build 2.17.0-79.0.dev)
    • DevTools version 2.10.0

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\andre\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.9)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.32106.194
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.63.2)
    • VS Code at C:\Users\andre\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.34.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22000.434]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 97.0.4692.71
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 97.0.1072.76

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

image

Metadata

Metadata

Assignees

Labels

f: material designflutter/packages/flutter/material repository.found in release: 2.10Found to occur in 2.10found in release: 2.8Found to occur in 2.8frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions