mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
I recommend reviewing each commit individually. The following were suppressed instead of migrated to minimize the time the tree is closed: 1. The [`Radio` -> `RadioGroup` migration](https://docs.flutter.dev/release/breaking-changes/radio-api-redesign). Tracked by: https://github.com/flutter/flutter/issues/179088. Part of: https://github.com/flutter/flutter/issues/178827 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] 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
292 lines
7.8 KiB
Dart
292 lines
7.8 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.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [SafeArea].
|
|
///
|
|
/// The app is wrapped with [Insets] (defined below)
|
|
/// to simulate a mobile device with a notched screen.
|
|
|
|
void main() => runApp(const Insets());
|
|
|
|
class SafeAreaExampleApp extends StatelessWidget {
|
|
const SafeAreaExampleApp({super.key});
|
|
|
|
static const Color spring = Color(0xFF00FF80);
|
|
static final ColorScheme colors = ColorScheme.fromSeed(seedColor: spring);
|
|
static final ThemeData theme = ThemeData(
|
|
colorScheme: colors,
|
|
sliderTheme: SliderThemeData(
|
|
trackHeight: 8,
|
|
activeTrackColor: colors.primary.withValues(alpha: 0.5),
|
|
thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 12),
|
|
),
|
|
scaffoldBackgroundColor: const Color(0xFFD0FFE8),
|
|
listTileTheme: const ListTileThemeData(
|
|
tileColor: Colors.white70,
|
|
visualDensity: VisualDensity(horizontal: -4, vertical: -4),
|
|
),
|
|
appBarTheme: AppBarTheme(
|
|
backgroundColor: colors.secondary,
|
|
foregroundColor: colors.onSecondary,
|
|
),
|
|
);
|
|
|
|
static final AppBar appBar = AppBar(title: const Text('SafeArea Demo'));
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
theme: theme,
|
|
debugShowCheckedModeBanner: false,
|
|
home: Builder(
|
|
builder: (BuildContext context) => Scaffold(
|
|
appBar: Toggle.appBar.of(context) ? appBar : null,
|
|
body: const DefaultTextStyle(
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
color: Colors.black,
|
|
),
|
|
child: Center(child: SafeAreaExample()),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SafeAreaExample extends StatelessWidget {
|
|
const SafeAreaExample({super.key});
|
|
|
|
static final Widget controls = Column(
|
|
children: <Widget>[
|
|
const SizedBox(height: 6),
|
|
Builder(
|
|
builder: (BuildContext context) => Text(
|
|
Toggle.safeArea.of(context) ? 'safe area!' : 'no safe area',
|
|
style: const TextStyle(fontSize: 24),
|
|
),
|
|
),
|
|
const Spacer(flex: 2),
|
|
for (final Value data in Value.allValues) ...data.controls,
|
|
],
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final bool hasSafeArea = Toggle.safeArea.of(context);
|
|
|
|
return SafeArea(
|
|
top: hasSafeArea,
|
|
bottom: hasSafeArea,
|
|
left: hasSafeArea,
|
|
right: hasSafeArea,
|
|
child: controls,
|
|
);
|
|
}
|
|
}
|
|
|
|
sealed class Value implements Enum {
|
|
Object _getValue(covariant Model<Value> model);
|
|
|
|
List<Widget> get controls;
|
|
|
|
static const List<Value> allValues = <Value>[
|
|
...Inset.values,
|
|
...Toggle.values,
|
|
];
|
|
}
|
|
|
|
enum Inset implements Value {
|
|
top('top notch'),
|
|
sides('side padding'),
|
|
bottom('bottom indicator');
|
|
|
|
const Inset(this.label);
|
|
|
|
final String label;
|
|
|
|
@override
|
|
double _getValue(_InsetModel model) => switch (this) {
|
|
top => model.insets.top,
|
|
sides => model.insets.left,
|
|
bottom => model.insets.bottom,
|
|
};
|
|
|
|
double of(BuildContext context) =>
|
|
_getValue(Model.of<_InsetModel>(context, this));
|
|
|
|
@override
|
|
List<Widget> get controls => <Widget>[
|
|
Text(label),
|
|
Builder(
|
|
builder: (BuildContext context) => Slider(
|
|
max: 50,
|
|
value: of(context),
|
|
onChanged: (double newValue) {
|
|
InsetsState.instance.changeInset(this, newValue);
|
|
},
|
|
),
|
|
),
|
|
const Spacer(),
|
|
];
|
|
}
|
|
|
|
enum Toggle implements Value {
|
|
appBar('Build an AppBar?'),
|
|
safeArea("Wrap Scaffold's body with SafeArea?");
|
|
|
|
const Toggle(this.label);
|
|
|
|
final String label;
|
|
|
|
@override
|
|
bool _getValue(_ToggleModel model) => switch (this) {
|
|
appBar => model.buildAppBar,
|
|
safeArea => model.buildSafeArea,
|
|
};
|
|
|
|
bool of(BuildContext context) =>
|
|
_getValue(Model.of<_ToggleModel>(context, this));
|
|
|
|
@override
|
|
List<Widget> get controls => <Widget>[
|
|
Builder(
|
|
builder: (BuildContext context) => SwitchListTile(
|
|
title: Text(label),
|
|
value: of(context),
|
|
onChanged: (bool value) {
|
|
InsetsState.instance.toggle(this, value);
|
|
},
|
|
),
|
|
),
|
|
];
|
|
}
|
|
|
|
abstract class Model<E extends Value> extends InheritedModel<E> {
|
|
const Model({super.key, required super.child});
|
|
|
|
static M of<M extends Model<Value>>(BuildContext context, Value value) {
|
|
return context.dependOnInheritedWidgetOfExactType<M>(aspect: value)!;
|
|
}
|
|
|
|
@override
|
|
bool updateShouldNotify(Model<E> oldWidget) => true;
|
|
|
|
@override
|
|
bool updateShouldNotifyDependent(Model<E> oldWidget, Set<E> dependencies) {
|
|
return dependencies.any(
|
|
(E data) => data._getValue(this) != data._getValue(oldWidget),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _InsetModel extends Model<Inset> {
|
|
const _InsetModel({required this.insets, required super.child});
|
|
|
|
final EdgeInsets insets;
|
|
}
|
|
|
|
class _ToggleModel extends Model<Toggle> {
|
|
_ToggleModel({required Set<Toggle> togglers, required super.child})
|
|
: buildAppBar = togglers.contains(Toggle.appBar),
|
|
buildSafeArea = togglers.contains(Toggle.safeArea);
|
|
|
|
final bool buildAppBar;
|
|
final bool buildSafeArea;
|
|
}
|
|
|
|
class Insets extends UniqueWidget<InsetsState> {
|
|
const Insets() : super(key: const GlobalObjectKey<InsetsState>('insets'));
|
|
|
|
@override
|
|
InsetsState createState() => InsetsState();
|
|
}
|
|
|
|
class InsetsState extends State<Insets> {
|
|
static InsetsState get instance => const Insets().currentState!;
|
|
|
|
EdgeInsets insets = const EdgeInsets.fromLTRB(8, 25, 8, 12);
|
|
void changeInset(Inset inset, double value) {
|
|
setState(() {
|
|
insets = switch (inset) {
|
|
Inset.top => insets.copyWith(top: value),
|
|
Inset.sides => insets.copyWith(left: value, right: value),
|
|
Inset.bottom => insets.copyWith(bottom: value),
|
|
};
|
|
});
|
|
}
|
|
|
|
final Set<Toggle> _togglers = <Toggle>{};
|
|
void toggle(Toggle toggler, bool value) {
|
|
setState(() {
|
|
value ? _togglers.add(toggler) : _togglers.remove(toggler);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final Widget topNotch = ClipRRect(
|
|
borderRadius: BorderRadius.vertical(bottom: Radius.circular(insets.top)),
|
|
child: SizedBox(
|
|
height: insets.top,
|
|
child: const FractionallySizedBox(
|
|
widthFactor: 1 / 2,
|
|
child: ColoredBox(color: Colors.black),
|
|
),
|
|
),
|
|
);
|
|
final Widget bottomIndicator = SizedBox(
|
|
width: double.infinity,
|
|
height: insets.bottom,
|
|
child: const FractionallySizedBox(
|
|
heightFactor: 0.5,
|
|
widthFactor: 0.5,
|
|
child: PhysicalShape(
|
|
clipper: ShapeBorderClipper(shape: StadiumBorder()),
|
|
color: Color(0xC0000000),
|
|
child: SizedBox.expand(),
|
|
),
|
|
),
|
|
);
|
|
final Widget sideBar = SizedBox(
|
|
width: insets.left,
|
|
height: double.infinity,
|
|
child: const IgnorePointer(child: ColoredBox(color: Colors.black12)),
|
|
);
|
|
|
|
final Widget app = _ToggleModel(
|
|
togglers: _togglers,
|
|
child: Builder(
|
|
builder: (BuildContext context) => MediaQuery(
|
|
data: MediaQuery.of(context).copyWith(
|
|
viewInsets: EdgeInsets.only(top: insets.top),
|
|
viewPadding: insets,
|
|
padding: insets,
|
|
),
|
|
child: const SafeAreaExampleApp(),
|
|
),
|
|
),
|
|
);
|
|
|
|
return _InsetModel(
|
|
insets: insets,
|
|
child: Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Stack(
|
|
children: <Widget>[
|
|
app,
|
|
Align(alignment: Alignment.topCenter, child: topNotch),
|
|
Align(alignment: Alignment.bottomCenter, child: bottomIndicator),
|
|
Align(alignment: Alignment.centerLeft, child: sideBar),
|
|
Align(alignment: Alignment.centerRight, child: sideBar),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|