Skip to content

Commit c12d120

Browse files
authored
Provide a way to override global InheritedWidgets (flutter#14348)
For example, so that the gallery can override the media query globally.
1 parent 12ceaef commit c12d120

File tree

10 files changed

+529
-207
lines changed

10 files changed

+529
-207
lines changed

examples/flutter_gallery/lib/gallery/app.dart

+14-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class GalleryAppState extends State<GalleryApp> {
5151
bool _showPerformanceOverlay = false;
5252
bool _checkerboardRasterCacheImages = false;
5353
bool _checkerboardOffscreenLayers = false;
54+
TextDirection _overrideDirection = TextDirection.ltr;
5455
double _timeDilation = 1.0;
5556
TargetPlatform _platform;
5657

@@ -139,6 +140,12 @@ class GalleryAppState extends State<GalleryApp> {
139140
_textScaleFactor = value;
140141
});
141142
},
143+
overrideDirection: _overrideDirection,
144+
onOverrideDirectionChanged: (TextDirection value) {
145+
setState(() {
146+
_overrideDirection = value;
147+
});
148+
},
142149
onSendFeedback: widget.onSendFeedback,
143150
);
144151

@@ -155,7 +162,7 @@ class GalleryAppState extends State<GalleryApp> {
155162
// using named routes, consider the example in the Navigator class documentation:
156163
// https://docs.flutter.io/flutter/widgets/Navigator-class.html
157164
_kRoutes[item.routeName] = (BuildContext context) {
158-
return _applyScaleFactor(item.buildRoute(context));
165+
return item.buildRoute(context);
159166
};
160167
}
161168

@@ -168,6 +175,12 @@ class GalleryAppState extends State<GalleryApp> {
168175
checkerboardOffscreenLayers: _checkerboardOffscreenLayers,
169176
routes: _kRoutes,
170177
home: _applyScaleFactor(home),
178+
builder: (BuildContext context, Widget child) {
179+
return new Directionality(
180+
textDirection: _overrideDirection,
181+
child: _applyScaleFactor(child),
182+
);
183+
},
171184
);
172185
}
173186
}

examples/flutter_gallery/lib/gallery/drawer.dart

+16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class GalleryDrawer extends StatelessWidget {
120120
this.checkerboardOffscreenLayers,
121121
this.onCheckerboardOffscreenLayersChanged,
122122
this.onPlatformChanged,
123+
this.overrideDirection: TextDirection.ltr,
124+
this.onOverrideDirectionChanged,
123125
this.onSendFeedback,
124126
}) : assert(onThemeChanged != null),
125127
assert(onTimeDilationChanged != null),
@@ -145,6 +147,9 @@ class GalleryDrawer extends StatelessWidget {
145147

146148
final ValueChanged<TargetPlatform> onPlatformChanged;
147149

150+
final TextDirection overrideDirection;
151+
final ValueChanged<TextDirection> onOverrideDirectionChanged;
152+
148153
final VoidCallback onSendFeedback;
149154

150155
@override
@@ -220,6 +225,16 @@ class GalleryDrawer extends StatelessWidget {
220225
selected: timeDilation != 1.0,
221226
);
222227

228+
final Widget overrideDirectionItem = new CheckboxListTile(
229+
title: const Text('Force RTL'),
230+
value: overrideDirection == TextDirection.rtl,
231+
onChanged: (bool value) {
232+
onOverrideDirectionChanged(value ? TextDirection.rtl : TextDirection.ltr);
233+
},
234+
secondary: const Icon(Icons.format_textdirection_r_to_l),
235+
selected: overrideDirection == TextDirection.rtl,
236+
);
237+
223238
final Widget sendFeedbackItem = new ListTile(
224239
leading: const Icon(Icons.report),
225240
title: const Text('Send feedback'),
@@ -285,6 +300,7 @@ class GalleryDrawer extends StatelessWidget {
285300
allDrawerItems.addAll(textSizeItems);
286301

287302
allDrawerItems..addAll(<Widget>[
303+
overrideDirectionItem,
288304
const Divider(),
289305
animateSlowlyItem,
290306
const Divider(),

examples/flutter_gallery/lib/gallery/home.dart

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class GalleryHome extends StatefulWidget {
7777
this.checkerboardOffscreenLayers,
7878
this.onCheckerboardOffscreenLayersChanged,
7979
this.onPlatformChanged,
80+
this.overrideDirection: TextDirection.ltr,
81+
this.onOverrideDirectionChanged,
8082
this.onSendFeedback,
8183
}) : assert(onThemeChanged != null),
8284
assert(onTimeDilationChanged != null),
@@ -102,6 +104,9 @@ class GalleryHome extends StatefulWidget {
102104

103105
final ValueChanged<TargetPlatform> onPlatformChanged;
104106

107+
final TextDirection overrideDirection;
108+
final ValueChanged<TextDirection> onOverrideDirectionChanged;
109+
105110
final VoidCallback onSendFeedback;
106111

107112
@override
@@ -177,6 +182,8 @@ class GalleryHomeState extends State<GalleryHome> with SingleTickerProviderState
177182
checkerboardOffscreenLayers: widget.checkerboardOffscreenLayers,
178183
onCheckerboardOffscreenLayersChanged: widget.onCheckerboardOffscreenLayersChanged,
179184
onPlatformChanged: widget.onPlatformChanged,
185+
overrideDirection: widget.overrideDirection,
186+
onOverrideDirectionChanged: widget.onOverrideDirectionChanged,
180187
onSendFeedback: widget.onSendFeedback,
181188
),
182189
body: new CustomScrollView(

examples/flutter_gallery/test/drawer_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void main() {
6969
expect(newTextSize, equals(origTextSize));
7070

7171
// Scroll to the bottom of the menu.
72-
await tester.drag(find.text('Small'), const Offset(0.0, -450.0));
72+
await tester.drag(find.text('Small'), const Offset(0.0, -1000.0));
7373
await tester.pump();
7474
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
7575

examples/flutter_gallery/test/smoke_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Future<Null> runSmokeTest(WidgetTester tester) async {
159159
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
160160

161161
// Scroll the 'Send feedback' item into view.
162-
await tester.drag(find.text('Small'), const Offset(0.0, -450.0));
162+
await tester.drag(find.text('Small'), const Offset(0.0, -1000.0));
163163
await tester.pump();
164164
await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
165165

0 commit comments

Comments
 (0)