颤振 - 使用提供程序 + 更改通知程序更改主题颜色(主要色板)



我按照YouTube上的教程将Flutter MaterialApp从浅色模式更改为深色模式(反之亦然(,创建者将Provider插件与ChangeNotifier和notifyListeners((一起使用;

import 'package:flutter/material.dart';

class ThemeChanger with ChangeNotifier{
ThemeData _themeData;
ThemeChanger(this._themeData);
getTheme() => _themeData;
setTheme (ThemeData theme) {
_themeData = theme;
notifyListeners();
}
}

然后,点击另一个页面上的按钮,

import 'package:flutter/material.dart';
import 'package:gatorblocks_rbt/utils/themeChanger.dart';
import 'package:provider/provider.dart';
class GeneralSettings extends StatelessWidget {
@override
Widget build(BuildContext context) {
ThemeChanger _themeChanger = Provider.of<ThemeChanger>(context);
return new Scaffold(
appBar: AppBar(
title: Text('General'),
),
body: ListView(
children: <Widget>[
Card(
child: ListTile(
leading: Icon(Icons.brightness_3, size: 50.0),
title: Text("Dark Mode"),
subtitle: Text("Change the app to a dark mode theme!"),
onTap: () {
_themeChanger.setTheme(ThemeData.dark());
},
)
),
Card(
child: ListTile(
leading: Icon(Icons.brightness_5, size: 50.0),
title: Text("Light Mode"),
subtitle: Text("Change the app to a light mode theme!"),
onTap: () {
_themeChanger.setTheme(ThemeData.light());
},
)
),
],
)
);
}
}

有谁知道如何将原色色板颜色从默认蓝色更改为其他颜色?

抱歉,我是编程新手,真的不知道如何解释/知道该怎么做。

在这一行:

_themeChanger.setTheme(ThemeData.light());

用:

_themeChanger.setTheme(ThemeData(brightness: Brightness.Light,primarySwatch: Colors.Red,);

相关内容

  • 没有找到相关文章

最新更新