Directionality widget (#11739)

This commit is contained in:
Ian Hickson 2017-08-23 10:34:20 -07:00 committed by GitHub
parent 9d4dd10f63
commit bf47b34b60
2 changed files with 119 additions and 0 deletions

View file

@ -55,6 +55,56 @@ export 'package:flutter/rendering.dart' show
WrapAlignment,
WrapCrossAlignment;
// BIDIRECTIONAL TEXT SUPPORT
/// A widget that determines the ambient directionality of text and
/// text-direction-sensitive render objects.
///
/// For example, [Padding] depends on the [Directionality] to resolve
/// [EdgeInsetsDirectional] objects into absolute [EdgeInsets] objects.
class Directionality extends InheritedWidget {
/// Creates a widget that determines the directionality of text and
/// text-direction-sensitive render objects.
///
/// The [textDirection] and [child] arguments must not be null.
const Directionality({
Key key,
@required this.textDirection,
@required Widget child
}) : assert(textDirection != null),
assert(child != null),
super(key: key, child: child);
/// The text direction for this subtree.
final TextDirection textDirection;
/// The text direction from the closest instance of this class that encloses
/// the given context.
///
/// If there is no [Directionality] ancestor widget in the tree at the given
/// context, then this will return null.
///
/// Typical usage is as follows:
///
/// ```dart
/// TextDirection textDirection = Directionality.of(context);
/// ```
static TextDirection of(BuildContext context) {
final Directionality widget = context.inheritFromWidgetOfExactType(Directionality);
return widget?.textDirection;
}
@override
bool updateShouldNotify(Directionality old) => textDirection != old.textDirection;
@override
void debugFillProperties(DiagnosticPropertiesBuilder description) {
super.debugFillProperties(description);
description.add(new EnumProperty<TextDirection>('textDirection', textDirection));
}
}
// PAINTING NODES
/// A widget that makes its child partially transparent.

View file

@ -0,0 +1,69 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
void main() {
testWidgets('Directionality', (WidgetTester tester) async {
final List<TextDirection> log = <TextDirection>[];
final Widget inner = new Builder(
builder: (BuildContext context) {
log.add(Directionality.of(context));
return new Placeholder();
}
);
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.ltr,
child: inner,
),
);
expect(log, <TextDirection>[TextDirection.ltr]);
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.ltr,
child: inner,
),
);
expect(log, <TextDirection>[TextDirection.ltr]);
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.rtl,
child: inner,
),
);
expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl]);
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.rtl,
child: inner,
),
);
expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl]);
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.ltr,
child: inner,
),
);
expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl, TextDirection.ltr]);
});
testWidgets('Directionality default', (WidgetTester tester) async {
bool good = false;
await tester.pumpWidget(new Builder(
builder: (BuildContext context) {
expect(Directionality.of(context), isNull);
good = true;
return new Placeholder();
},
));
expect(good, isTrue);
});
testWidgets('Directionality can\'t be null', (WidgetTester tester) async {
expect(() { new Directionality(textDirection: null, child: new Placeholder()); }, throwsAssertionError);
});
}