首先在您的材料主题中设置为条件(可能是您定义这是在主类中)
import 'dart:async';
import 'package:flutter/material.dart';
StreamController<bool> setTheme = StreamController();
main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<bool>(
initialData: true,
stream: setTheme.stream,
builder: (context, snapshot) {
return MaterialApp(
theme: snapshot.data ? ThemeData.light() : ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text("Your Theme")),
body: YourButtonPage()));/*Change this name with Your class*/
});
}
}
class YourButtonPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: Icon(
Icons.brightness_low,
size: 21,
),
alignment: Alignment.center,
onPressed: () {
setTheme.add(true);
}),
IconButton(
icon: Icon(
Icons.brightness_1,
size: 21,
),
alignment: Alignment.center,
onPressed: () {
setTheme.add(false);
}),
])));
}
}