[Breaking Change] ObstructingPreferredSizeWidget public interface change

166 views
Skip to first unread message

Weiyu Huang

unread,
Sep 17, 2019, 6:20:34 PM9/17/19
to Flutter Public Announcements (flutter-announce)
I'd like to propose a breaking change to ObstructingPreferredSizeWidget, as detailed in https://github.com/flutter/flutter/pull/40690/files, that will alter the interface of ObstructingPreferredSizeWidget

from
abstract class ObstructingPreferredSizeWidget extends PreferredSizeWidget {
 
/// If true, this widget fully obstructs widgets behind it by the specified  
 
/// size.  
 
///  
 
/// If false, this widget partially obstructs.  
 
bool get fullObstruction;  
}


to
abstract class ObstructingPreferredSizeWidget extends PreferredSizeWidget {
 
/// If true, this widget fully obstructs widgets behind it by the specified
 
/// size.
 
///
 
/// If false, this widget partially obstructs.
 
bool shouldFullyObstruct(BuildContext context);
}

The ObstructingPreferredSizeWidget protocol is mainly used by CupertinoPageScaffold to determine whether the navigation bar should obstruct the content in the background.
But the attribute is not necessarily "intrinsic", often times it depends on the BuildContext the widget is in. For example, whether a widget is fullObstruction may depend on a color that needs to be retrieved from the current theme, 
or the color itself needs to be resolved using the current BuildContext. Adding a BuildContext parameter would allow these common use cases to be implemented without having to resort to hacks like this (returning null in fullObstruction):
     
 final bool fullObstruction = widget.navigationBar.fullObstruction ?? CupertinoTheme.of(context).barBackgroundColor.alpha == 0xFF;



Migration Guide

This change only affects code that extended or implemented ObstructingPreferredSizeWidget. Migration is usually as simple as changing the interface and keep the implementation as is:

bool shouldFullyObstruct(BuildContext context) => fullObstruction;

Please note that it's no longer allow to return null in shouldFullyObstruct. If you have any concerns/suggestions, or need help migrating your code, please leave a comment in https://github.com/flutter/flutter/pull/40690 or contact me by email. Thank you!

Reply all
Reply to author
Forward
0 new messages