1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'dart:math' as math; |
6 | |
7 | import 'package:flutter/material.dart'; |
8 | import 'package:flutter/services.dart'; |
9 | |
10 | /// Flutter code sample for setting the [SystemUiOverlayStyle] with an [AnnotatedRegion]. |
11 | |
12 | void main() => runApp(const SystemOverlayStyleApp()); |
13 | |
14 | class SystemOverlayStyleApp extends StatelessWidget { |
15 | const SystemOverlayStyleApp({super.key}); |
16 | |
17 | @override |
18 | Widget build(BuildContext context) { |
19 | return const MaterialApp(home: SystemOverlayStyleExample()); |
20 | } |
21 | } |
22 | |
23 | class SystemOverlayStyleExample extends StatefulWidget { |
24 | const SystemOverlayStyleExample({super.key}); |
25 | |
26 | @override |
27 | State<SystemOverlayStyleExample> createState() => _SystemOverlayStyleExampleState(); |
28 | } |
29 | |
30 | class _SystemOverlayStyleExampleState extends State<SystemOverlayStyleExample> { |
31 | final math.Random _random = math.Random(); |
32 | SystemUiOverlayStyle _currentStyle = SystemUiOverlayStyle.light; |
33 | |
34 | void _changeColor() { |
35 | final Color color = Color.fromRGBO( |
36 | _random.nextInt(255), |
37 | _random.nextInt(255), |
38 | _random.nextInt(255), |
39 | 1.0, |
40 | ); |
41 | setState(() { |
42 | _currentStyle = SystemUiOverlayStyle.dark.copyWith( |
43 | statusBarColor: color, |
44 | systemNavigationBarColor: color, |
45 | ); |
46 | }); |
47 | } |
48 | |
49 | @override |
50 | Widget build(BuildContext context) { |
51 | return AnnotatedRegion<SystemUiOverlayStyle>( |
52 | value: _currentStyle, |
53 | child: Scaffold( |
54 | body: Column( |
55 | crossAxisAlignment: CrossAxisAlignment.start, |
56 | children: <Widget>[ |
57 | Padding( |
58 | padding: const EdgeInsets.all(16.0), |
59 | child: Text( |
60 | 'SystemUiOverlayStyle Sample', |
61 | style: Theme.of(context).textTheme.titleLarge, |
62 | ), |
63 | ), |
64 | Expanded( |
65 | child: Center( |
66 | child: ElevatedButton(onPressed: _changeColor, child: const Text('Change Color')), |
67 | ), |
68 | ), |
69 | ], |
70 | ), |
71 | ), |
72 | ); |
73 | } |
74 | } |
75 |