123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- import 'package:crab_ui/folder_drawer.dart';
- import 'package:crab_ui/structs.dart';
- import 'package:flutter/widgets.dart';
- import 'api_service.dart';
- import 'package:flutter/material.dart';
- import 'email.dart';
- // import 'serialize.dart';
- class HomeScreen extends StatefulWidget {
- @override
- _HomeScreenState createState() => _HomeScreenState();
- }
- class _HomeScreenState extends State<HomeScreen> with TickerProviderStateMixin {
- final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
- final GlobalKey<EmailPageState> _emailPageKey = GlobalKey<EmailPageState>();
- ApiService apiService = ApiService();
- bool _isSidebarOpen = true;
- bool querySearches = false;
- String? _selectedOption = "INBOX";
- List<String> _tabs = ['Emails'];
- Map<String, Widget> _tabWidgets = {};
- TabController? _tabController;
- @override
- void initState() {
- super.initState();
- _tabController = TabController(length: _tabs.length, vsync: this);
- _tabWidgets['Emails'] = EmailPage(
- key: _emailPageKey,
- );
- }
- // Add a new tab based on the search
- void _performSearch(String query, String? list) {
- setState(() {
- if (!_tabs.contains(query)) {
- _tabs.add(query);
- _tabWidgets[query] = _buildSearchResultsWidget(
- query, list); // Store a different widget for this tab
- _tabController = TabController(length: _tabs.length, vsync: this);
- }
- });
- }
- void _showOptionsSearchDialog () async {
- List<String> folders = await apiService.fetchFolders();
- if (mounted) {
- showDialog(
- context: context,
- builder: (BuildContext context) {
- return AlertDialog(
- title: Text('Choose an Option'),
- content: Column(
- mainAxisSize: MainAxisSize.min,
- children: folders.map((option) {
- return ListTile(
- title: Text(option),
- leading: Radio<String>(
- value: option,
- groupValue: _selectedOption, // Bind with _selectedOption
- onChanged: (String? value) {
- setState(() {
- _selectedOption = value;
- });
- Navigator.of(context).pop(); // Close the dialog on selection
- },
- ),
- );
- }).toList(),
- ),
- actions: <Widget>[
- ElevatedButton(
- child: Text('Submit'),
- onPressed: () {
- Navigator.of(context).pop(); // Close the dialog
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- content: Text('You selected: $_selectedOption'),
- ));
- },
- ),
- ],
- );
- },
- );}
- }
-
- // Remove a tab
- void _removeTab(int index) {
- if (_tabs[index] != 'Emails') {
- setState(() {
- String tabToRemove = _tabs[index];
- _tabs.removeAt(index);
- _tabWidgets
- .remove(tabToRemove); // Remove widget associated with the tab
- _tabController = TabController(length: _tabs.length, vsync: this);
- });
- }
- }
- // Build a custom widget for each search query
- Widget _buildSearchResultsWidget(String query, String? list) {
- return FutureBuilder<List<SerializableMessage>>(
- future: apiService.sonicSearch(list ?? "INBOX", 50, 0, query),
- builder: (BuildContext context,
- AsyncSnapshot<List<SerializableMessage>> snapshot) {
- if (snapshot.connectionState == ConnectionState.waiting) {
- return Center(child: CircularProgressIndicator());
- } else if (snapshot.hasError) {
- return Center(child: Text('Error: ${snapshot.error}'));
- } else if (!snapshot.hasData || snapshot.data!.isEmpty) {
- return Center(child: Text('No results found for: $query'));
- } else {
- List<SerializableMessage> result = snapshot.data!;
- return Scaffold(
- body: ListView.separated(
- itemCount: result.length,
- itemBuilder: (context, index) {
- final email = result[index];
- return ListTile(
- title: Text(email.from,
- style: TextStyle(fontWeight: FontWeight.bold)),
- subtitle: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [Text(email.subject)],
- ),
- trailing: Text(email.date.toString()),
- onTap: () async {
- // print('tapped');
- String emailContent =
- await apiService.fetchEmailContent([email.id]);
- // print('content below');
- // print(emailContent);
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => EmailView(
- emailContent: emailContent,
- from: email.from,
- name: email.name,
- to: email.to.toString(),
- subject: email.subject,
- date: email.date.toString(),
- id: email.id.toString(),
- ),
- ),
- );
- },
- );
- },
- separatorBuilder: (context, index) => Divider(),
- ),
- // child: Column(
- // mainAxisAlignment: MainAxisAlignment.center,
- // children: [
- // Text("Results for: $query", style: TextStyle(fontSize: 24)),
- // // Display the actual data
- // Text(result[0].name), // Accessing the first result safely
- // Text(result[0].from), // Displaying the 'from' field as an example
- // Text(result[0].hash),
- // Text(result[0].subject),
- // Text(result[0].uid.toString()),
- // Text(result[0].list),
- // Text(result[0].id),
- // // Add more fields or customize the display
- // // SerializableEmailListScreen(emails: result, getEmailContent: getEmailContent)
- // // Expanded(
- // // child:
- // // ),
- // ],
- );
- // );
- }
- },
- );
- }
- @override
- void dispose() {
- _tabController?.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: _scaffoldKey,
- drawer: FolderDrawer(
- apiService: apiService,
- onFolderTap: (folder) {
- _emailPageKey.currentState?.updateSelectedFolder(folder);
- },
- ),
- body: Stack(
- children: [
- Row(
- children: [
- // Sidebar
- if (_isSidebarOpen)
- Container(
- width: 70,
- color: Color.fromARGB(17, 96, 122, 135),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- ListTile(
- leading: Icon(Icons.home),
- onTap: () {
- // Navigate to Home
- },
- ),
- ListTile(
- leading: Icon(Icons.settings),
- onTap: () {
- // Navigate to Settings
- },
- ),
- ListTile(
- leading: Icon(Icons.email),
- onTap: () {
- _scaffoldKey.currentState?.openDrawer();
- },
- ),
- Spacer(),
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: Align(
- alignment: Alignment.bottomLeft,
- child: IconButton(
- icon: Icon(Icons.close, color: Colors.white),
- onPressed: () {
- setState(() {
- _isSidebarOpen = false;
- });
- },
- ),
- ),
- ),
- ],
- ),
- ),
- // Main content
- Expanded(
- child: Column(
- children: [
- Container(
- padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 4.0),
- color: Color.fromARGB(42, 36, 102, 132),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Container(
- width: 800,
- height: 40,
- child: TextField(
- decoration: InputDecoration(
- hintText: 'Search...',
- border: OutlineInputBorder(),
- prefixIcon: Icon(Icons.search),
- ),
- onSubmitted: (value) {
- if (value.isNotEmpty) {
- _performSearch(value, _selectedOption);
- }
- //this is the input box i mentioned
- // if (value == '') {
- // setState(() {
- // querySearches = false;
- // });
- // }
- // Future<List<String>> results = apiService
- // .sonicSearch('INBOX', 20, 0, value);
- // // print(value);
- // print(results);
- // setState(() {
- // querySearches = true;
- // });
- },
- ),
- ),
- SizedBox(
- width: 16,
- ),
- Container(
- width: 80,
- height: 40,
- child: ElevatedButton(
- onPressed: _showOptionsSearchDialog,
- child: Icon(Icons.manage_search),
- ),
- )
- ],
- ),
- ),
- Container(
- padding: EdgeInsets.all(0.0),
- color: Color.fromARGB(42, 36, 102, 132),
- child: Row(
- children: [
- Container(
- height: 2,
- )
- ],
- ),
- ),
- Container(
- color: Color.fromARGB(255, 131, 110, 143),
- child: TabBar(
- controller: _tabController,
- isScrollable: true,
- tabs: _tabs
- .asMap()
- .entries
- .map((entry) => Tab(
- child: Row(
- children: [
- Text(entry.value),
- if (entry.value != 'Emails')
- GestureDetector(
- onTap: () => _removeTab(entry.key),
- child: Icon(Icons.close, size: 16),
- ),
- ],
- ),
- ))
- .toList(),
- labelColor: Colors.white,
- indicatorColor: Colors.white,
- ),
- ),
- Container(
- // alignment: Alignment.topLeft,
- padding: EdgeInsets.all(8.0),
- color: Colors.white,
- child: Row(
- children: [
- ElevatedButton(
- onPressed: () {
- _emailPageKey.currentState
- ?.updatePagenation('back');
- },
- child: Icon(Icons.navigate_before),
- ),
- Text(_emailPageKey.currentState?.getPage() ?? '1'),
- ElevatedButton(
- onPressed: () {
- _emailPageKey.currentState
- ?.updatePagenation('next');
- },
- child: Icon(Icons.navigate_next),
- ),
- ],
- ),
- ),
- Expanded(
- child: TabBarView(
- controller: _tabController,
- children: _tabs.map((tab) {
- return _tabWidgets[tab] ??
- Center(child: Text("No content found"));
- // return Center(
- // child: EmailPage(
- // key: _emailPageKey,
- // ));
- }).toList(),
- ),
- ),
- // if (_tabs.isEmpty)
- // Expanded(
- // child: EmailPage(key: _emailPageKey),
- // ),
- // if (_tabs.isNotEmpty)
- // Expanded(
- // // child: Text('supposed to be mails'),
- // child: TabBarView(
- // controller: _tabController,
- // children: _tabs
- // .map((tab) => Center(child: Text('Results for $tab')))
- // .toList(),
- // ),
- // ),
- ],
- ),
- ),
- ],
- ),
- if (!_isSidebarOpen)
- Positioned(
- bottom: 16,
- left: 16,
- child: FloatingActionButton(
- child: Icon(Icons.menu),
- onPressed: () {
- setState(() {
- _isSidebarOpen = true;
- });
- },
- ),
- ),
- ],
- ),
- );
- }
- }
- // void _showPopupMenu(BuildContext context, Offset position) async {
- // final RenderBox overlay =
- // Overlay.of(context).context.findRenderObject() as RenderBox;
- // await showMenu<String>(
- // context: context,
- // position: RelativeRect.fromLTRB(
- // position.dx,
- // position.dy,
- // overlay.size.width - position.dx,
- // overlay.size.height - position.dy,
- // ),
- // items: <PopupMenuEntry<String>>[
- // PopupMenuItem<String>(
- // value: 'Open',
- // child: Text('Open'),
- // ),
- // PopupMenuItem<String>(
- // value: 'Reply',
- // child: Text('Reply'),
- // ),
- // PopupMenuItem<String>(
- // value: 'Delete',
- // child: Text('Delete'),
- // ),
- // ],
- // );
- // }
- // }
|