-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a custom shape example for AppBar.shape
#146421
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// 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. | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
/// Flutter code sample for [AppBar.shape]. | ||
|
||
void main() => runApp(const AppBarExampleApp()); | ||
|
||
class AppBarExampleApp extends StatelessWidget { | ||
const AppBarExampleApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const MaterialApp( | ||
home: AppBarExample(), | ||
); | ||
} | ||
} | ||
|
||
class AppBarExample extends StatelessWidget { | ||
const AppBarExample({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final ColorScheme colorScheme = Theme.of(context).colorScheme; | ||
|
||
return Scaffold( | ||
appBar: AppBar( | ||
shape: const CustomAppBarShape(), | ||
backgroundColor: colorScheme.primaryContainer, | ||
title: const Text('AppBar Sample'), | ||
bottom: PreferredSize( | ||
preferredSize: const Size.fromHeight(64.0), | ||
child: Padding( | ||
padding: const EdgeInsets.symmetric(horizontal: 16.0), | ||
child: TextField( | ||
decoration: InputDecoration( | ||
border: OutlineInputBorder( | ||
borderSide: BorderSide(color: colorScheme.primary), | ||
), | ||
enabledBorder: OutlineInputBorder( | ||
borderSide: BorderSide(color: colorScheme.onPrimaryContainer), | ||
), | ||
filled: true, | ||
hintText: 'Enter a search term', | ||
fillColor: colorScheme.surface, | ||
prefixIcon: Icon( | ||
Icons.search_rounded, | ||
color: colorScheme.primary | ||
), | ||
suffixIcon: Icon( | ||
Icons.tune_rounded, | ||
color: colorScheme.primary | ||
), | ||
) | ||
), | ||
), | ||
), | ||
), | ||
body: ListView.builder( | ||
padding: const EdgeInsets.only(top: 45.0), | ||
itemCount: 20, | ||
itemBuilder: (BuildContext context, int index) { | ||
return ListTile( | ||
title: Text('Item $index'), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class CustomAppBarShape extends OutlinedBorder { | ||
// Implementing the constructor allows the CustomAppBarShape to be | ||
// properly compared when calling the `identical` method. | ||
const CustomAppBarShape({ super.side }); | ||
|
||
Path _getPath(Rect rect) { | ||
final Path path = Path(); | ||
final Size size = Size(rect.width, rect.height * 1.5) ; | ||
|
||
final double p0 = size.height * 0.75; | ||
path.lineTo(0.0, p0); | ||
|
||
final Offset controlPoint = Offset(size.width * 0.4, size.height); | ||
final Offset endPoint = Offset(size.width, size.height / 2); | ||
path.quadraticBezierTo(controlPoint.dx, controlPoint.dy, endPoint.dx, endPoint.dy); | ||
|
||
path.lineTo(size.width, 0.0); | ||
path.close(); | ||
|
||
return path; | ||
} | ||
|
||
@override | ||
Path getOuterPath(Rect rect, {TextDirection? textDirection}) { | ||
return _getPath(rect.inflate(side.width)); | ||
} | ||
|
||
@override | ||
Path getInnerPath(Rect rect, {TextDirection? textDirection}) { | ||
return _getPath(rect); | ||
} | ||
|
||
@override | ||
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) { | ||
if (rect.isEmpty) { | ||
return; | ||
} | ||
canvas.drawPath( | ||
getOuterPath(rect, textDirection: textDirection), | ||
side.toPaint(), | ||
); | ||
} | ||
|
||
@override | ||
ShapeBorder scale(double t) { | ||
return CustomAppBarShape(side: side.scale(t)); | ||
} | ||
|
||
@override | ||
OutlinedBorder copyWith({BorderSide? side}) { | ||
return CustomAppBarShape(side: side ?? this.side); | ||
} | ||
|
||
// The lerpFrom method is necessary for the CustomAppBarShape to be | ||
// properly animated when changing the AppBar shape and when | ||
// the AppBar is rebuilt. | ||
@override | ||
ShapeBorder? lerpFrom(ShapeBorder? a, double t) { | ||
if (a is CustomAppBarShape) { | ||
return CustomAppBarShape(side: BorderSide.lerp(a.side, side, t)); | ||
} | ||
return super.lerpFrom(a, t); | ||
} | ||
|
||
// The lerpTo method is necessary for the CustomAppBarShape to be | ||
// properly animated when changing the AppBar shape and when | ||
// the AppBar is rebuilt. | ||
@override | ||
ShapeBorder? lerpTo(ShapeBorder? b, double t) { | ||
if (b is CustomAppBarShape) { | ||
return CustomAppBarShape(side: BorderSide.lerp(b.side, side, t)); | ||
} | ||
return super.lerpTo(b, t); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_api_samples/material/app_bar/app_bar.4.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('AppBar uses custom shape', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.AppBarExampleApp(), | ||
); | ||
|
||
Material getMaterial() => tester.widget<Material>(find.descendant( | ||
of: find.byType(AppBar), | ||
matching: find.byType(Material), | ||
)); | ||
expect(getMaterial().shape, const example.CustomAppBarShape()); | ||
}); | ||
|
||
testWidgets('AppBar bottom contains TextField', (WidgetTester tester) async { | ||
await tester.pumpWidget( | ||
const example.AppBarExampleApp(), | ||
); | ||
|
||
final Finder textFieldFinder = find.descendant( | ||
of: find.byType(AppBar), | ||
matching: find.byType(TextField), | ||
); | ||
|
||
expect(textFieldFinder, findsOneWidget); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful shape!