SystemChrome
sample.packages.flutter.lib.src.services.system_chrome.563.
The following example creates a widget that changes the status bar color to a random value on Android.
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
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,
);
});
}
@override
Widget build(BuildContext context) {
return AnnotatedRegion(
value: _currentStyle,
child: Center(
child: ElevatedButton(
onPressed: _changeColor,
child: const Text('Change Color'),
),
),
);
}
}
SHARE: