Line data Source code
1 : import 'package:cwtch/models/servers.dart';
2 : import 'package:cwtch/views/addeditservers.dart';
3 : import 'package:cwtch/widgets/passwordfield.dart';
4 : import 'package:cwtch/widgets/serverrow.dart';
5 : import 'package:flutter/material.dart';
6 : import 'package:provider/provider.dart';
7 : import 'package:cwtch/l10n/app_localizations.dart';
8 :
9 : import '../cwtch_icons_icons.dart';
10 : import '../main.dart';
11 : import '../settings.dart';
12 :
13 : ///
14 : class ServersView extends StatefulWidget {
15 0 : @override
16 0 : _ServersView createState() => _ServersView();
17 : }
18 :
19 : class _ServersView extends State<ServersView> {
20 : final ctrlrPassword = TextEditingController();
21 :
22 0 : @override
23 : void dispose() {
24 0 : ctrlrPassword.dispose();
25 0 : super.dispose();
26 : }
27 :
28 0 : @override
29 : Widget build(BuildContext context) {
30 0 : return Scaffold(
31 0 : backgroundColor: Provider.of<Settings>(context, listen: false).theme.backgroundMainColor,
32 0 : appBar: AppBar(
33 0 : title: Text(MediaQuery.of(context).size.width > 600 ? AppLocalizations.of(context)!.serversManagerTitleLong : AppLocalizations.of(context)!.serversManagerTitleShort),
34 0 : actions: getActions(),
35 : ),
36 0 : floatingActionButton: FloatingActionButton(
37 0 : onPressed: _pushAddServer,
38 0 : tooltip: AppLocalizations.of(context)!.addServerTooltip,
39 0 : child: Icon(Icons.add, semanticLabel: AppLocalizations.of(context)!.addServerTooltip, color: Provider.of<Settings>(context).theme.defaultButtonTextColor),
40 : ),
41 0 : body: Consumer<ServerListState>(
42 0 : builder: (context, svrs, child) {
43 0 : final tiles = svrs.servers.map((ServerInfoState server) {
44 0 : return ChangeNotifierProvider<ServerInfoState>.value(
45 : value: server,
46 0 : builder: (context, child) => RepaintBoundary(child: ServerRow()),
47 : );
48 : });
49 :
50 0 : final divided = ListTile.divideTiles(context: context, tiles: tiles).toList();
51 :
52 0 : if (tiles.isEmpty) {
53 0 : return Center(child: Text(AppLocalizations.of(context)!.unlockServerTip, textAlign: TextAlign.center));
54 : }
55 :
56 0 : return ListView(children: divided);
57 : },
58 : ),
59 : );
60 : }
61 :
62 0 : List<Widget> getActions() {
63 0 : List<Widget> actions = new List<Widget>.empty(growable: true);
64 :
65 : // Unlock Profiles
66 0 : actions.add(
67 0 : IconButton(
68 0 : icon: Icon(CwtchIcons.lock_open_24px),
69 0 : color: Provider.of<ServerListState>(context).servers.isEmpty ? Provider.of<Settings>(context).theme.defaultButtonColor : Provider.of<Settings>(context).theme.mainTextColor,
70 0 : tooltip: AppLocalizations.of(context)!.tooltipUnlockProfiles,
71 0 : onPressed: _modalUnlockServers,
72 : ),
73 : );
74 :
75 : return actions;
76 : }
77 :
78 0 : void _modalUnlockServers() {
79 0 : showModalBottomSheet<void>(
80 0 : context: context,
81 : isScrollControlled: true,
82 0 : builder: (BuildContext context) {
83 0 : return Padding(
84 0 : padding: MediaQuery.of(context).viewInsets,
85 0 : child: RepaintBoundary(
86 0 : child: Container(
87 : height: 200, // bespoke value courtesy of the [TextField] docs
88 0 : child: Center(
89 0 : child: Padding(
90 0 : padding: EdgeInsets.all(10.0),
91 0 : child: Column(
92 : mainAxisAlignment: MainAxisAlignment.center,
93 : mainAxisSize: MainAxisSize.min,
94 0 : children: <Widget>[
95 0 : Text(AppLocalizations.of(context)!.enterServerPassword),
96 0 : SizedBox(height: 20),
97 0 : CwtchPasswordField(
98 : autofocus: true,
99 0 : controller: ctrlrPassword,
100 0 : action: unlock,
101 0 : validator: (value) {
102 : return null;
103 : },
104 : ),
105 0 : SizedBox(height: 20),
106 0 : Row(
107 : mainAxisAlignment: MainAxisAlignment.spaceEvenly,
108 0 : children: [
109 0 : Spacer(),
110 0 : Expanded(
111 0 : child: ElevatedButton(
112 0 : child: Text(AppLocalizations.of(context)!.unlock, semanticsLabel: AppLocalizations.of(context)!.unlock),
113 0 : onPressed: () {
114 0 : unlock(ctrlrPassword.value.text);
115 : },
116 : ),
117 : ),
118 0 : Spacer(),
119 : ],
120 : ),
121 : ],
122 : ),
123 : ),
124 : ),
125 : ),
126 : ),
127 : );
128 : },
129 : );
130 : }
131 :
132 0 : void unlock(String password) {
133 0 : Provider.of<FlwtchState>(context, listen: false).cwtch.LoadServers(password);
134 0 : ctrlrPassword.text = "";
135 0 : Navigator.pop(context);
136 : }
137 :
138 0 : void _pushAddServer() {
139 0 : Navigator.of(context).push(
140 0 : PageRouteBuilder(
141 0 : pageBuilder: (bcontext, a1, a2) {
142 0 : return MultiProvider(
143 0 : providers: [
144 0 : ChangeNotifierProvider<ServerInfoState>(
145 0 : create: (_) => ServerInfoState(onion: "", serverBundle: "", description: "", autoStart: true, running: false, isEncrypted: true),
146 : ),
147 : ],
148 0 : child: AddEditServerView(),
149 : );
150 : },
151 0 : transitionsBuilder: (c, anim, a2, child) => FadeTransition(opacity: anim, child: child),
152 0 : transitionDuration: Duration(milliseconds: 200),
153 : ),
154 : );
155 : }
156 : }
|