SystemUiOverlayStyle, add examples and improve documentation (#122187)
SystemUiOverlayStyle, add two examples and improve documentation
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
// 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.
|
||||
|
||||
// Flutter code sample for [AppBar.systemOverlayStyle].
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const SystemOverlayStyleApp());
|
||||
|
||||
class SystemOverlayStyleApp extends StatelessWidget {
|
||||
const SystemOverlayStyleApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
home: const SystemOverlayStyleExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SystemOverlayStyleExample extends StatefulWidget {
|
||||
const SystemOverlayStyleExample({super.key});
|
||||
|
||||
@override
|
||||
State<SystemOverlayStyleExample> createState() => _SystemOverlayStyleExampleState();
|
||||
}
|
||||
|
||||
class _SystemOverlayStyleExampleState extends State<SystemOverlayStyleExample> {
|
||||
final math.Random _random = math.Random();
|
||||
SystemUiOverlayStyle _currentStyle = SystemUiOverlayStyle.light;
|
||||
|
||||
void _changeColor() {
|
||||
final Color color = Color.fromRGBO(
|
||||
_random.nextInt(255),
|
||||
_random.nextInt(255),
|
||||
_random.nextInt(255),
|
||||
1.0,
|
||||
);
|
||||
setState(() {
|
||||
_currentStyle = SystemUiOverlayStyle.dark.copyWith(
|
||||
statusBarColor: color,
|
||||
systemNavigationBarColor: color,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('SystemUiOverlayStyle Sample'),
|
||||
systemOverlayStyle: _currentStyle,
|
||||
),
|
||||
body: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: _changeColor,
|
||||
child: const Text('Change Color'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,37 +2,38 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for [SystemChrome.setSystemUIOverlayStyle].
|
||||
// Flutter code sample for setting the [SystemUiOverlayStyle] with an [AnnotatedRegion].
|
||||
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() => runApp(const SystemOverlayStyleApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
class SystemOverlayStyleApp extends StatelessWidget {
|
||||
const SystemOverlayStyleApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: MyStatefulWidget(),
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
home: const SystemOverlayStyleExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({super.key});
|
||||
class SystemOverlayStyleExample extends StatefulWidget {
|
||||
const SystemOverlayStyleExample({super.key});
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
State<SystemOverlayStyleExample> createState() => _SystemOverlayStyleExampleState();
|
||||
}
|
||||
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
class _SystemOverlayStyleExampleState extends State<SystemOverlayStyleExample> {
|
||||
final math.Random _random = math.Random();
|
||||
SystemUiOverlayStyle _currentStyle = SystemUiOverlayStyle.light;
|
||||
|
||||
@@ -46,6 +47,7 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
setState(() {
|
||||
_currentStyle = SystemUiOverlayStyle.dark.copyWith(
|
||||
statusBarColor: color,
|
||||
systemNavigationBarColor: color,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -54,10 +56,26 @@ class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
Widget build(BuildContext context) {
|
||||
return AnnotatedRegion<SystemUiOverlayStyle>(
|
||||
value: _currentStyle,
|
||||
child: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: _changeColor,
|
||||
child: const Text('Change Color'),
|
||||
child: Scaffold(
|
||||
body: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
'SystemUiOverlayStyle Sample',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: ElevatedButton(
|
||||
onPressed: _changeColor,
|
||||
child: const Text('Change Color'),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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/services.dart';
|
||||
import 'package:flutter_api_samples/services/system_chrome/system_chrome.set_system_u_i_overlay_style.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('AppBar.systemOverlayStyle can change system overlays styles.', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.SystemOverlayStyleApp(),
|
||||
);
|
||||
|
||||
final SystemUiOverlayStyle? firstStyle = SystemChrome.latestStyle;
|
||||
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
final SystemUiOverlayStyle? secondStyle = SystemChrome.latestStyle;
|
||||
expect(secondStyle?.statusBarColor, isNot(firstStyle?.statusBarColor));
|
||||
expect(secondStyle?.systemNavigationBarColor, isNot(firstStyle?.systemNavigationBarColor));
|
||||
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
final SystemUiOverlayStyle? thirdStyle = SystemChrome.latestStyle;
|
||||
expect(thirdStyle?.statusBarColor, isNot(secondStyle?.statusBarColor));
|
||||
expect(thirdStyle?.systemNavigationBarColor, isNot(secondStyle?.systemNavigationBarColor));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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/services.dart';
|
||||
import 'package:flutter_api_samples/services/system_chrome/system_chrome.set_system_u_i_overlay_style.1.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('AnnotatedRegion can change system overlays style.', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.SystemOverlayStyleApp(),
|
||||
);
|
||||
|
||||
final SystemUiOverlayStyle? firstStyle = SystemChrome.latestStyle;
|
||||
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
final SystemUiOverlayStyle? secondStyle = SystemChrome.latestStyle;
|
||||
expect(secondStyle?.statusBarColor, isNot(firstStyle?.statusBarColor));
|
||||
expect(secondStyle?.systemNavigationBarColor, isNot(firstStyle?.systemNavigationBarColor));
|
||||
|
||||
await tester.tap(find.byType(ElevatedButton));
|
||||
await tester.pump();
|
||||
final SystemUiOverlayStyle? thirdStyle = SystemChrome.latestStyle;
|
||||
expect(thirdStyle?.statusBarColor, isNot(secondStyle?.statusBarColor));
|
||||
expect(thirdStyle?.systemNavigationBarColor, isNot(secondStyle?.systemNavigationBarColor));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user