import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyThemePage());
class MyThemePage extends StatelessWidget {
// iOS 浅色主题
final ThemeData kIOSTheme = ThemeData(
brightness: Brightness.light,
accentColor: Colors.white,
primaryColor: Colors.blue,
iconTheme: IconThemeData(color: Colors.grey),
textTheme: TextTheme(body1: TextStyle(color: Colors.black)));
// Android 深色主题
final ThemeData kAndroidTheme = ThemeData(
brightness: Brightness.dark, // 深色主题
accentColor: Colors.black, //(按钮)Widget 前景色为黑色
primaryColor: Colors.cyan, // 主题色为青色
iconTheme: IconThemeData(color: Colors.blue), //icon 主题色为蓝色
textTheme: TextTheme(body1: TextStyle(color: Colors.red)) // 文本主题色为红色
);
Widget build(BuildContext context) {
var theme = MyTheme(
title: 'Flutter Demo Home Page',
themes: [kIOSTheme, kAndroidTheme],
);
return MaterialApp(
title: 'My Theme',
theme: kAndroidTheme,
home: theme,
);
}
}
class MyTheme extends StatefulWidget {
MyTheme({Key key, this.title, this.themes}) : super(key: key);
final String title;
final List<ThemeData> themes;
_MyThemeState createState() => _MyThemeState();
}
class _MyThemeState extends State<MyTheme> {
bool isLight;
@override
void initState() {
isLight = true;
super.initState();
}
@override
Widget build(BuildContext context) {
return Theme(
child: Scaffold(
appBar: AppBar(
title: Text('Theme switch'),
),
floatingActionButton: FloatingActionButton(
child: Text(isLight ? 'Night' : 'Light'),
onPressed: () {
setState(() {
isLight = !isLight;
});
},
),
),
data: isLight ? widget.themes[0] : widget.themes[1]);
}
}
展开