我有一个主屏幕,在标签栏视图的主屏幕内我返回了一个聊天页面 在此聊天页面中,当用户长按单击时,我想更改主屏幕中应用程序栏的操作。为此,我创建了一个静态变量,该变量在主屏幕内命名为 selected,这样我就可以更改聊天页面小部件中的值,然后如果所选值为 true,我将有一个带有操作小部件的应用程序栏,否则我不会有操作。 这是我的代码我该如何解决这个问题。这是主屏幕课程
static bool selected = false;
TabBarView(
controller: controller,
children: [
const CommunityPage(),
ChatPage(
// in here I want to change the value of selected variable
chatModels: widget.chatModels,
),
const StatusPage(),
const CallsPage(),
],
),
此活动将更改 HomeScreen.selected,如果它是 true 或 false 它会执行此操作
actions: [
HomeScreen.selected
? Row(
children: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (_) =>
ThemeConsumer(child: ThemeDialog()));
},
icon: Icon(
Icons.palette,
color: ThemeManeger().checkIsDark(context)
? Colors.grey
: Colors.white60,
),
),
IconButton(
onPressed: () {},
icon: Icon(FeatherIcons.camera,
color: ThemeManeger().checkIsDark(context)
? Colors.grey
: Colors.white70)),
IconButton(
icon: Icon(
Icons.search,
color: ThemeManeger().checkIsDark(context)
? Colors.grey
: Colors.white70,
),
onPressed: () {},
),
PopupMenuButton(
color: ThemeManeger().checkIsDark(context)
? appBarColor
: Colors.white,
icon: Icon(Icons.more_vert,
color: ThemeManeger().checkIsDark(context)
? Colors.grey
: Colors.white),
onSelected: (value) {
if (value == 0) {
Navigator.pushNamed(context, CreateGroup.routName);
}
},
itemBuilder: (BuildContext context) {
return [
PopupMenuItem(
value: 0,
child: Text(
"New group",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
),
PopupMenuItem(
child: Text(
"New broadcast",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
),
PopupMenuItem(
child: Text(
"Linked devices",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
),
PopupMenuItem(
child: Text(
"Starred messages",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
),
PopupMenuItem(
child: Text(
"Settings",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
),
];
})
],
)
: Row()
],
这是我的 ChatPage 代码,其中应该更改 homeScreen.selected 值
import 'dart:math';
import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:whatsapp/Model/ChatModel.dart';
import 'package:whatsapp/Screens/chat/chatScreen.dart';
import 'package:whatsapp/constants/colors.dart';
import '../CustomUI/show_image_fullScreen.dart';
import '../Screens/contacts/select_contact.dart';
import '../Screens/home/HomeScreen.dart';
import '../utils/theme_manager.dart';
class ChatPage extends StatefulWidget {
const ChatPage({Key? key, required this.chatModels}) : super(key: key);
final List<ChatModel> chatModels;
static List<int> selectedIndexList = [];
@override
State<ChatPage> createState() => _ChatPageState();
}
class _ChatPageState extends State<ChatPage> {
List<Contact>? contacts;
@override
void initState() {
super.initState();
getAllContacts();
}
void getAllContacts() async {
bool isGranted = await Permission.contacts.isGranted;
if (!isGranted) {
isGranted = await Permission.contacts.request().isGranted;
}
List myContacts = (await ContactsService.getContacts()).toList();
setState(() {
contacts = myContacts.cast<Contact>();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, SelectContact.routName);
},
child: Icon(Icons.chat, color: Colors.white),
),
body: contacts == null
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: contacts!.length,
itemBuilder: (BuildContext context, index) {
int randomNumber = Random().nextInt(colors.length);
return Column(
children: [
const SizedBox(
height: 30,
),
GestureDetector(
onLongPress: () {
if (!HomeScreen.selected) {
setState(() {
HomeScreen.selected = true;
ChatPage.selectedIndexList.add(index);
});
} else {
if (ChatPage.selectedIndexList.contains(index)) {
setState(() {
ChatPage.selectedIndexList.remove(index);
});
} else {
setState(() {
ChatPage.selectedIndexList.add(index);
});
}
}
if (ChatPage.selectedIndexList.isEmpty) {
setState(() {
HomeScreen.selected = false;
});
}
},
child: ListTile(
leading: InkWell(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return ShowImageFullScreen(
sorceChat: widget.chatModels[0],
image: Image.asset("assets/1.jpg"),
tabBarOptions: false,
);
}));
},
child: (contacts![index].avatar == null)
? CircleAvatar(
backgroundColor: Colors.grey,
radius: 30,
child: SvgPicture.asset(
"assets/person.svg",
color: Colors.grey,
width: 38,
height: 38,
),
)
: Stack(
children: [
CircleAvatar(
backgroundColor: colors[randomNumber]
['color'],
radius: 30,
child: Text(
contacts![index].initials(),
style: TextStyle(
color: colors[randomNumber]
['mode'] ==
'light'
? Colors.black54
: Colors.white),
),
),
HomeScreen.selected &&
ChatPage.selectedIndexList.contains(index)
? Positioned(
bottom: 0,
right: 0,
child: CircleAvatar(
backgroundColor: ThemeManeger()
.checkIsDark(context)
? tabColor
: Colors.teal,
radius: 11,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: ThemeManeger()
.checkIsDark(
context)
? Colors.black
: Colors.white,
width: 2),
borderRadius:
BorderRadius.circular(
11)),
child: Icon(
Icons.check,
color: ThemeManeger()
.checkIsDark(
context)
? Colors.white
: Colors.black,
size: 18,
),
),
),
)
: const Positioned(child: SizedBox())
],
)),
onTap: () {
if (!HomeScreen.selected) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChatScreen(
chatModel: widget.chatModels[index],
),
),
);
} else {
if (ChatPage.selectedIndexList.contains(index)) {
setState(() {
ChatPage.selectedIndexList.remove(index);
});
} else {
setState(() {
ChatPage.selectedIndexList.add(index);
});
}
}
if (ChatPage.selectedIndexList.isEmpty) {
setState(() {
HomeScreen.selected = false;
});
}
},
title: Text(
contacts![index].displayName!,
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white
: Colors.black),
),
subtitle: Row(
children: [
Icon(
Icons.done_all,
color: ThemeManeger().checkIsDark(context)
? Colors.white70
: Colors.black45,
),
Text(
"سلام",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white70
: Colors.black45),
),
],
),
trailing: Text(
"3:00",
style: TextStyle(
color: ThemeManeger().checkIsDark(context)
? Colors.white70
: Colors.black45,
fontSize: 12),
),
),
),
],
);
},
),
);
}
}
我强烈建议不要为此使用静态变量。
您所描述的是状态管理问题的一个明显例子。我建议您从 Flutter 文档开始对此进行一些研究。
然后由您决定采用哪种框架或方法(Provider、GetX、Redux、Riverpod...)。我将从实现
ChangeNotifier
和 Provider
开始,因为它们很容易理解并且完美适合您的用例。