很简单
的背景由一个名为HomeBackground的小部件管理。所以我只想从浮动操作按钮中获得一些属性。
以下是背景:
class _IconsBackground extends StatefulWidget {
final bool isLoading;
final IconData firstIcon;
const _IconsBackground({required this.firstIcon, required this.isLoading});
@override
State<_IconsBackground> createState() => _IconsBackgroundState();
}
class _IconsBackgroundState extends State<_IconsBackground> {
bool isDarkMode = false;
Color iconsColor = AppTheme.primary;
List<Color> background = [
const Color.fromARGB(255, 234, 231, 231),
Colors.grey
];
swtichTheme() {
isDarkMode = !isDarkMode;
if (isDarkMode) {
iconsColor = Colors.white;
background = const [
Color.fromARGB(255, 64, 64, 64),
Color.fromARGB(255, 0, 0, 0)
];
} else {
iconsColor = AppTheme.primary;
background = [const Color.fromARGB(255, 234, 231, 231), Colors.grey];
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Container( // more things
因此,switchTheme()方法应该从主屏幕中的浮动操作按钮调用:
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final BlogsProvider provider = Provider.of<BlogsProvider>(context);
return Scaffold(
appBar: const BlogAppBar(),
floatingActionButton: const **_UserActionsButton()**,
body: **HomeBackground**(
firstIcon: Icons.chat,
isLoading: false,
child: RefreshIndicator(
onRefresh: () async {
provider.getBlogs();
},
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Column(
children: _loadBlogs(provider),
),
),
)));
}
List<BlogCard> _loadBlogs(BlogsProvider provider) {
List<Blog> blogsSorted = provider.blogs;
blogsSorted.sort((a, b) => a.created.compareTo(b.created));
return blogsSorted.reversed.toList().map((e) => BlogCard(blog: e)).toList();
}
}
我该如何解决这个问题?
1条答案
按热度按时间vql8enpb1#
有两种简单的方法可以做到这一点:
1.在树的较高位置设置一个StatefulWidget,例如在HomeScreen中,然后将更改主题和当前主题的方法传递给FloatingActionButton。此外,将当前主题发送给Background小部件。
1.您已经在使用Provider包,请为主题创建其他提供程序,并将当前主题和更改方法存储在该提供程序中。