首先,在你的材料主题中,将"first"设置为条件(可能是在主类中定义)。
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()));
});
}
}
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);
}),
])));
}
}