Flutter 我的自定义主题更改事件仅更改一次主题
你好,我是新来的。我试图理解颤振的集团结构。 我正试图用一个简单的天气应用程序来改变我的主题Flutter 我的自定义主题更改事件仅更改一次主题,flutter,dart,Flutter,Dart,你好,我是新来的。我试图理解颤振的集团结构。 我正试图用一个简单的天气应用程序来改变我的主题 final _currentWeather = weatherState.weather; final _weatherAbbr = _currentWeather.consolidatedWeather[0].weatherStateAbbr; context.watch<ThemeBloc>().add(ThemeChangeEvent(weatherAbbr: _weatherAbbr
final _currentWeather = weatherState.weather;
final _weatherAbbr = _currentWeather.consolidatedWeather[0].weatherStateAbbr;
context.watch<ThemeBloc>().add(ThemeChangeEvent(weatherAbbr: _weatherAbbr));
我的集团
类别:
abstract class ThemeEvent extends Equatable {
const ThemeEvent(List<String> list);
List<Object> get props => [];
}
class ThemeChangeEvent extends ThemeEvent {
final String weatherAbbr;
ThemeChangeEvent({@required this.weatherAbbr}) : super([weatherAbbr]);
}
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
ThemeBloc() : super(ApplicationThemeLoaded(theme: ThemeData.light(), color: Colors.blue));
@override
Stream<ThemeState> mapEventToState(
ThemeEvent event,
) async* {
// TODO: implement mapEventToState
ApplicationThemeLoaded applicationTheme;
if (event is ThemeChangeEvent) {
switch(event.weatherAbbr) {
case "sn" :
case "sl" :
case "h" :
case "t" :
case "hc" :
applicationTheme = ApplicationThemeLoaded(theme: ThemeData(primaryColor: Colors.blueGrey), color: Colors.grey);
break;
case "hr" :
case "lr" :
case "s" :
applicationTheme = ApplicationThemeLoaded(theme: ThemeData(primaryColor: Colors.indigoAccent), color: Colors.indigo);
break;
case "lc" :
case "c" :
applicationTheme = ApplicationThemeLoaded(theme: ThemeData(primaryColor: Colors.orangeAccent), color: Colors.yellow);
break;
}
yield applicationTheme;
}
}
}
abstract class ThemeState extends Equatable {
const ThemeState(List<Object> list);
@override
List<Object> get props => [];
}
class ApplicationThemeLoaded extends ThemeState {
final ThemeData theme;
final MaterialColor color;
ApplicationThemeLoaded({@required this.theme, @required this.color}) : super([theme, color]);
}
这是我的main.dart
课程,我正在尝试更改主题:
void main() {
setupLocator();
runApp(BlocProvider<ThemeBloc>(
create: (context) => ThemeBloc(),
child: MyApp(),));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, ThemeState themeState) =>
MaterialApp(
title: 'Weather App',
debugShowCheckedModeBanner: false,
theme: (themeState as ApplicationThemeLoaded).theme,
home: BlocProvider<WeatherBloc>(
create: (context) => WeatherBloc(),
child: WeatherApp()),
),
);
}
}
void main(){
setupLocator();
runApp(BlocProvider(
create:(context)=>ThemeBloc(),
child:MyApp(),);
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回BlocBuilder(
生成器:(上下文,themstate themstate)=>
材料聚丙烯(
标题:“天气应用程序”,
debugShowCheckedModeBanner:false,
主题:(themeState作为应用程序已加载)。主题,
首页:BlocProvider(
create:(context)=>WeatherBloc(),
child:WeatherApp()),
),
);
}
}
应检查您的应用程序的主题
和颜色
,以进行比较
将它们包括在道具中
:
class ApplicationThemeLoaded extends ThemeState {
final ThemeData theme;
final MaterialColor color;
ApplicationThemeLoaded({@required this.theme, @required this.color}) : super([theme, color]);
@override
List<Object> get props => [theme,color];
}
类应用程序已加载扩展了属性{
最终主题数据主题;
最终材料颜色;
ApplicationThemeLoaded({@required this.theme,@required this.color}):超级([theme,color]);
@凌驾
列表获取道具=>[主题,颜色];
}
这将有助于说明当前状态与前一状态不同,从而触发状态更新