117 lines
3.3 KiB
Dart
117 lines
3.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:repertory/helpers.dart';
|
|
import 'package:repertory/models/mount.dart';
|
|
|
|
class MountWidget extends StatefulWidget {
|
|
const MountWidget({super.key});
|
|
|
|
@override
|
|
State<MountWidget> createState() => _MountWidgetState();
|
|
}
|
|
|
|
class _MountWidgetState extends State<MountWidget> {
|
|
Timer? _timer;
|
|
bool _enabled = true;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Card(
|
|
child: Consumer<Mount>(
|
|
builder: (context, mount, widget) {
|
|
final textColor = Colors.blue;
|
|
final subTextColor = Colors.black;
|
|
|
|
final isActive = mount.state == Icons.toggle_on;
|
|
final nameText = SelectableText(
|
|
formatMountName(mount.type, mount.name),
|
|
style: TextStyle(color: subTextColor),
|
|
);
|
|
|
|
return ListTile(
|
|
isThreeLine: isActive,
|
|
leading: IconButton(
|
|
icon: Icon(Icons.settings, color: textColor),
|
|
onPressed: () {
|
|
Navigator.pushNamed(
|
|
context,
|
|
'/settings',
|
|
arguments: mount.mountConfig,
|
|
);
|
|
},
|
|
),
|
|
subtitle:
|
|
isActive
|
|
? Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
nameText,
|
|
SelectableText(
|
|
mount.path,
|
|
style: TextStyle(color: subTextColor),
|
|
),
|
|
],
|
|
)
|
|
: nameText,
|
|
title: SelectableText(
|
|
initialCaps(mount.type),
|
|
style: TextStyle(color: textColor, fontWeight: FontWeight.bold),
|
|
),
|
|
trailing: IconButton(
|
|
icon: Icon(
|
|
mount.state,
|
|
color: isActive ? Colors.blue : Colors.grey,
|
|
),
|
|
onPressed:
|
|
_enabled
|
|
? () {
|
|
setState(() {
|
|
_enabled = false;
|
|
});
|
|
|
|
if (isActive) {
|
|
mount
|
|
.mount(isActive)
|
|
.then((_) {
|
|
setState(() {
|
|
_enabled = true;
|
|
});
|
|
})
|
|
.catchError((_) {
|
|
setState(() {
|
|
_enabled = true;
|
|
});
|
|
});
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_enabled = true;
|
|
});
|
|
}
|
|
: null,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_timer = Timer.periodic(const Duration(seconds: 5), (_) {
|
|
Provider.of<Mount>(context, listen: false).refresh();
|
|
});
|
|
}
|
|
}
|