Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add DropdownTheme for DropdownButton and DropdownButtonFormField #104211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

TahaTesser
Copy link
Member

fixes #98695

minimal code sample
import 'package:flutter/material.dart';

const List<String> fruits = <String>['Mango', 'Apple', 'Banana', 'Strawberry'];

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

class MyApp extends StatelessWidget {
  const MyApp({super.key, this.dark = false, this.useMaterial3 = true});

  final bool dark;
  final bool useMaterial3;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      themeMode: dark ? ThemeMode.dark : ThemeMode.light,
      theme: ThemeData(
        useMaterial3: useMaterial3,
        brightness: Brightness.light,
        colorSchemeSeed: const Color(0xff6750a4),
        dropdownTheme: DropdownThemeData(
          dropdownColor: Colors.yellow,
          style: const TextStyle(color: Colors.red),
          iconColor: MaterialStateProperty.all(Colors.blue),
          focusColor: Colors.orange,
          borderRadius: BorderRadius.circular(20.0),
          inputDecorationTheme: const InputDecorationTheme(
            border: OutlineInputBorder(),
            labelStyle: TextStyle(color: Colors.deepPurpleAccent),
          ),
        ),
      ),
      darkTheme: ThemeData(
        useMaterial3: useMaterial3,
        brightness: Brightness.dark,
        colorSchemeSeed: const Color(0xff6750a4),
      ),
      home: const DropdownFormFieldSample(),
    );
  }
}

class DropdownFormFieldSample extends StatefulWidget {
  const DropdownFormFieldSample({super.key});

  @override
  State<DropdownFormFieldSample> createState() =>
      _DropdownFormFieldSampleState();
}

class _DropdownFormFieldSampleState extends State<DropdownFormFieldSample> {
  String? _dropdownbuttonFormValue;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DropdownButton Sample'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            DropdownButton<String>(
              hint: const Text('Hint'),
              borderRadius: BorderRadius.circular(12),
              autofocus: true,
              value: _dropdownbuttonFormValue,
              items: fruits
                  .map<DropdownMenuItem<String>>(
                    (String value) => DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    ),
                  )
                  .toList(),
              onChanged: (String? value) =>
                  setState(() => _dropdownbuttonFormValue = value),
            ),
            DropdownButtonFormField<String>(
              hint: const Text('Hint'),
              borderRadius: BorderRadius.circular(12),
              decoration: const InputDecoration(
                labelText: 'Select a fruit',
              ),
              autofocus: true,
              value: _dropdownbuttonFormValue,
              items: fruits
                  .map<DropdownMenuItem<String>>(
                    (String value) => DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    ),
                  )
                  .toList(),
              onChanged: (String? value) =>
                  setState(() => _dropdownbuttonFormValue = value),
            ),
          ],
        ),
      ),
    );
  }
}

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels May 19, 2022
@TahaTesser TahaTesser force-pushed the dropdown_theme branch 2 times, most recently from a87c70e to b0f08d8 Compare May 20, 2022 10:37
@TahaTesser TahaTesser requested a review from HansMuller May 20, 2022 11:12
@TahaTesser
Copy link
Member Author

TahaTesser commented May 20, 2022

cc: @Piinks
This PR adds DropdownTheme (also to deprecate ButtonTheme and its reference in the source code).

Once this is merged, I will add a replacement for ButtonTheme.of(context).alignedDropdown in this new theme.

I found that the dropdown_test.dart lack a specific test for this property so I will add this property with all the required tests in a separate PR. (this PR focused on theme and its tests)

Copy link
Contributor

@Piinks Piinks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wonderful!
I am curious about theme conventions for some of those static methods.
Since this is net new, we have the opportunity to make sure we pick the pattern we want to stick with moving forward.
@HansMuller @darrenaustin do you have any opinion on this?

Comment on lines +63 to +65
/// Overrides the default value of [DropdownButton.iconEnabledColor],
/// [DropdownButton.iconDisabledColor], [DropdownButtonFormField<T>.iconEnabledColor]
/// and [DropdownButtonFormField<T>.iconDisabledColor].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo these looks like potential deprecations in the future to migrate to a material state property? @darrenaustin is that true?

}

/// Linearly interpolate between DropdownThemeData objects.
static DropdownThemeData? lerp(DropdownThemeData? a, DropdownThemeData? b, double t) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can this return null? I have looked at a few different examples like the switch theme and checkbox theme and they do not do this. However, I do know there is some variance between theme classes based on new and old preferred patterns. Was there a class you were using as a model here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just saw ButtonStyle is similar, that is certainly newer. I wonder if there is a chance to create more consistency in these theme patterns. (Likely a separate issue :) )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are definitely not consistent about the return value when both parameters are null. Returning a non-null theme data value in that case does seem odd though. We should be (become) consistent.

@Piinks Piinks added the c: new feature Nothing broken; request for a new capability label Jun 7, 2022
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed the tests but this looks pretty good to me.

}

/// Linearly interpolate between DropdownThemeData objects.
static DropdownThemeData? lerp(DropdownThemeData? a, DropdownThemeData? b, double t) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are definitely not consistent about the return value when both parameters are null. Returning a non-null theme data value in that case does seem odd though. We should be (become) consistent.

@TahaTesser TahaTesser requested a review from Piinks June 10, 2022 15:13
@TahaTesser
Copy link
Member Author

Once this is merged, I will add support for the Material 3 dropdown menu using menu tokens with the theme defaults.

Already did for the Popup menu.

@HansMuller
Copy link
Contributor

CC @gspencergoog
Before landing this, we're going to need a plan for how DropdownButton et al., PopupMenuButton et al., and the MenuBar et al. components unify in terms of themes and features. Are all of these menu components entirely unique or do they have shared attributes and capabilities that should be reflected in their widget and theme APIs?

@HansMuller HansMuller requested a review from gspencergoog August 1, 2022 16:24
@Piinks
Copy link
Contributor

Piinks commented Sep 14, 2022

What is the status of this PR? @gspencergoog given @HansMuller's last comment do you have any input here as it relates to menus? Or is this ok to proceed?

@gspencergoog
Copy link
Contributor

I think unifying the menu architecture is something where we need to have some discussions and planning, and those haven't happened yet. I suspect that we will want some unification, but I can't say for sure yet what that will be.

As for this PR, I suspect that we will still want a separate theme for the Dropdown button, but if Hans thinks we should wait, then perhaps we should just close this PR for now and revive it when we have a plan.

@HansMuller
Copy link
Contributor

This was a good first cut, given the current menu tech in Flutter, however I think we should wait until the new menu system lands and we've decided how that will change the dropdown classes (#104211 (comment)). I'm going to close this PR for now; we'll revisit in a few weeks when the new menu system has landed.

@HansMuller HansMuller closed this Sep 19, 2022
@TahaTesser TahaTesser deleted the dropdown_theme branch September 19, 2022 17:51
@Priyantha-Kingslake
Copy link

Hi, what is the status of the dropdownMenu Theme?

@TahaTesser
Copy link
Member Author

Hi, what is the status of the dropdownMenu Theme?

Please try the M3 DropdownMenu. It's a newer version of DropdownButton and supports theming using DropdownMenuTheme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: new feature Nothing broken; request for a new capability f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add DropdownButtonTheme/Data
5 participants