mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
WIP Commits separated as follows: - Update lints in analysis_options files - Run `dart fix --apply` - Clean up leftover analysis issues - Run `dart format .` in the right places. Local analysis and testing passes. Checking CI now. Part of https://github.com/flutter/flutter/issues/178827 - Adoption of flutter_lints in examples/api coming in a separate change (cc @loic-sharma) ## 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]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
341 lines
12 KiB
Dart
341 lines
12 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// This file is run as part of a reduced test set in CI on Mac and Windows
|
|
// machines.
|
|
@Tags(<String>['reduced-test-set'])
|
|
library;
|
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../image_data.dart';
|
|
import '../painting/mocks_for_image_cache.dart';
|
|
|
|
void main() {
|
|
testWidgets('CircleAvatar with dark background color', (WidgetTester tester) async {
|
|
final Color backgroundColor = Colors.blue.shade900;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(backgroundColor: backgroundColor, radius: 50.0, child: const Text('Z')),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(backgroundColor));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
|
|
});
|
|
|
|
testWidgets('CircleAvatar with light background color', (WidgetTester tester) async {
|
|
final Color backgroundColor = Colors.blue.shade100;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(backgroundColor: backgroundColor, radius: 50.0, child: const Text('Z')),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(backgroundColor));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorDark));
|
|
});
|
|
|
|
testWidgets('CircleAvatar with image background', (WidgetTester tester) async {
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(
|
|
backgroundImage: MemoryImage(Uint8List.fromList(kTransparentImage)),
|
|
radius: 50.0,
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.image!.fit, equals(BoxFit.cover));
|
|
});
|
|
|
|
testWidgets('CircleAvatar with image foreground', (WidgetTester tester) async {
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(
|
|
foregroundImage: MemoryImage(Uint8List.fromList(kBlueRectPng)),
|
|
radius: 50.0,
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.image!.fit, equals(BoxFit.cover));
|
|
});
|
|
|
|
testWidgets('CircleAvatar backgroundImage is used as a fallback for foregroundImage', (
|
|
WidgetTester tester,
|
|
) async {
|
|
addTearDown(imageCache.clear);
|
|
final errorImage = ErrorImageProvider();
|
|
var caughtForegroundImageError = false;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: RepaintBoundary(
|
|
child: CircleAvatar(
|
|
foregroundImage: errorImage,
|
|
backgroundImage: MemoryImage(Uint8List.fromList(kBlueRectPng)),
|
|
radius: 50.0,
|
|
onForegroundImageError: (_, _) => caughtForegroundImageError = true,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
expect(caughtForegroundImageError, true);
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.image!.fit, equals(BoxFit.cover));
|
|
await expectLater(find.byType(CircleAvatar), matchesGoldenFile('circle_avatar.fallback.png'));
|
|
});
|
|
|
|
testWidgets('CircleAvatar with foreground color', (WidgetTester tester) async {
|
|
final Color foregroundColor = Colors.red.shade100;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(foregroundColor: foregroundColor, child: const Text('Z')),
|
|
),
|
|
);
|
|
|
|
final fallback = ThemeData.fallback();
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(40.0, 40.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(fallback.primaryColorDark));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(foregroundColor));
|
|
});
|
|
|
|
testWidgets('Material3 - CircleAvatar default colors', (WidgetTester tester) async {
|
|
final theme = ThemeData();
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: Theme(
|
|
data: theme,
|
|
child: const CircleAvatar(child: Text('Z')),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(theme.colorScheme.primaryContainer));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(theme.colorScheme.onPrimaryContainer));
|
|
});
|
|
|
|
testWidgets('CircleAvatar text does not expand with textScaler', (WidgetTester tester) async {
|
|
final Color foregroundColor = Colors.red.shade100;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(foregroundColor: foregroundColor, child: const Text('Z')),
|
|
),
|
|
);
|
|
|
|
expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
|
|
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: MediaQuery(
|
|
data: const MediaQueryData(
|
|
textScaler: TextScaler.linear(2.0),
|
|
size: Size(111.0, 111.0),
|
|
devicePixelRatio: 1.1,
|
|
padding: EdgeInsets.all(11.0),
|
|
),
|
|
child: CircleAvatar(
|
|
child: Builder(
|
|
builder: (BuildContext context) {
|
|
final MediaQueryData data = MediaQuery.of(context);
|
|
|
|
// These should not change.
|
|
expect(data.size, equals(const Size(111.0, 111.0)));
|
|
expect(data.devicePixelRatio, equals(1.1));
|
|
expect(data.padding, equals(const EdgeInsets.all(11.0)));
|
|
|
|
// This should be overridden to 1.0.
|
|
expect(data.textScaler, TextScaler.noScaling);
|
|
return const Text('Z');
|
|
},
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
|
|
});
|
|
|
|
testWidgets('CircleAvatar respects minRadius', (WidgetTester tester) async {
|
|
final Color backgroundColor = Colors.blue.shade900;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: UnconstrainedBox(
|
|
child: CircleAvatar(
|
|
backgroundColor: backgroundColor,
|
|
minRadius: 50.0,
|
|
child: const Text('Z'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(backgroundColor));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
|
|
});
|
|
|
|
testWidgets('CircleAvatar respects maxRadius', (WidgetTester tester) async {
|
|
final Color backgroundColor = Colors.blue.shade900;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(
|
|
backgroundColor: backgroundColor,
|
|
maxRadius: 50.0,
|
|
child: const Text('Z'),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(backgroundColor));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
|
|
});
|
|
|
|
testWidgets('CircleAvatar respects setting both minRadius and maxRadius', (
|
|
WidgetTester tester,
|
|
) async {
|
|
final Color backgroundColor = Colors.blue.shade900;
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: CircleAvatar(
|
|
backgroundColor: backgroundColor,
|
|
maxRadius: 50.0,
|
|
minRadius: 50.0,
|
|
child: const Text('Z'),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
expect(box.size, equals(const Size(100.0, 100.0)));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(backgroundColor));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
|
|
});
|
|
|
|
group('Material 2', () {
|
|
// These tests are only relevant for Material 2. Once Material 2
|
|
// support is deprecated and the APIs are removed, these tests
|
|
// can be deleted.
|
|
|
|
testWidgets('Material2 - CircleAvatar default colors with light theme', (
|
|
WidgetTester tester,
|
|
) async {
|
|
final theme = ThemeData(useMaterial3: false, primaryColor: Colors.grey.shade100);
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: Theme(
|
|
data: theme,
|
|
child: const CircleAvatar(child: Text('Z')),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(theme.primaryColorLight));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.titleLarge!.color));
|
|
});
|
|
|
|
testWidgets('Material2 - CircleAvatar default colors with dark theme', (
|
|
WidgetTester tester,
|
|
) async {
|
|
final theme = ThemeData(useMaterial3: false, primaryColor: Colors.grey.shade800);
|
|
await tester.pumpWidget(
|
|
wrap(
|
|
child: Theme(
|
|
data: theme,
|
|
child: const CircleAvatar(child: Text('Z')),
|
|
),
|
|
),
|
|
);
|
|
|
|
final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
|
|
final child = box.child! as RenderDecoratedBox;
|
|
final decoration = child.decoration as BoxDecoration;
|
|
expect(decoration.color, equals(theme.primaryColorDark));
|
|
|
|
final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
|
|
expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.titleLarge!.color));
|
|
});
|
|
});
|
|
|
|
testWidgets('CircleAvatar renders at zero area', (WidgetTester tester) async {
|
|
await tester.pumpWidget(
|
|
const MaterialApp(
|
|
home: SizedBox.shrink(child: CircleAvatar(child: Text('X'))),
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
Widget wrap({required Widget child}) {
|
|
return Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: MediaQuery(
|
|
data: const MediaQueryData(),
|
|
child: MaterialApp(
|
|
theme: ThemeData(useMaterial3: false),
|
|
home: Center(child: child),
|
|
),
|
|
),
|
|
);
|
|
}
|