mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
## Description This PR fixes some `DropdownButtonFormField` issues where the overlay color overflows. Before this PR, `DropdownButtonFormField` was relying on an `InkWell` to display overlay colors. This resulted in several issues related to the `InkWell` overflowing because it is not aware of the inner container inside `InputDecorator`, for instance see https://github.com/flutter/flutter/issues/106659. With this PR, `DropdownButtonFormField` does not use an `InkWell` but rely on `InputDecorator` to paint overlay colors. `InputDecorator` paints overlay colors only on its internal container, this fixes the color overflowing when using `InkWell`. With this change users can opt-in for overlay colors to be painted by setting InputDecorator.filled to true (similarly to TextField and accordingly to [the Material specification](https://m2.material.io/components/menus#dropdown-menu)). Code sample from https://github.com/flutter/flutter/issues/106659 with InputDecoration.filled set to true: <details><summary>Code sample with InputDecoration.filled set to true</summary> ```dart import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { MyApp({Key? key}) : super(key: key); static const String _title = 'Flutter Code Sample'; final _formKey = GlobalKey<FormState>(); @override Widget build(BuildContext context) { var items = [ 'Ayo', 'This', 'Don', 'Look', 'Right', ].map((String val) { return DropdownMenuItem( value: val, child: Text( val, ), ); }).toList(); return MaterialApp( title: _title, theme: ThemeData( inputDecorationTheme: const InputDecorationTheme( border: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(32)), borderSide: BorderSide(color: Colors.blue, width: 2), ), ), ), home: Scaffold( body: Center( child: SizedBox( width: 500, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Form( key: _formKey, child: DropdownButtonFormField( onTap: () { _formKey.currentState!.validate(); }, validator: (String? v) => 'Required', onChanged: (String? value) {}, items: items, // Set InputDecoration.filled to true if overlays should be visible. // See Material specification for filled vs outlined dropdown button: // https://m2.material.io/components/menus#dropdown-menu. decoration: const InputDecoration(filled: true), ), ), ], ), ), ), ), ); } } ``` </details> Before:  After:  After (when filled is not set to true):  ## Related Issue Fixes [DropdownButtonFormField InkWell spreads to error message](https://github.com/flutter/flutter/issues/106659). Fixes [DropdownButtonFormField input decorator focus/hover is not clipped and appears behind fill color.](https://github.com/flutter/flutter/issues/147069) First step for [DropDownButtonFormField hoverColor has no effect in web and desktop platforms](https://github.com/flutter/flutter/issues/151460) ## Tests Adds 4 tests. Updates 2 tests (remove checks specific to InkWell usage and use filled: true when checking for hover/focus colors). Removes 1 test (test specific to InkWell usage, because this PR removes the InkWell the test is obsolete).
Flutter
Flutter is a new way to build high-performance, cross-platform mobile, web, and desktop apps. Flutter is optimized for today's — and tomorrow's — mobile and desktop devices. We are focused on low-latency input and high frame rates on all platforms.
See the getting started guide for information about using Flutter.