This commit is contained in:
parent
343c324050
commit
52f0d755ba
@ -0,0 +1,10 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:repertory/types/mount_config.dart';
|
||||||
|
|
||||||
|
class Mount with ChangeNotifier {
|
||||||
|
final MountConfig mountConfig;
|
||||||
|
Mount(this.mountConfig);
|
||||||
|
|
||||||
|
String get name => mountConfig.name;
|
||||||
|
String get type => mountConfig.type;
|
||||||
|
}
|
@ -15,17 +15,6 @@ class MountList with ChangeNotifier {
|
|||||||
|
|
||||||
UnmodifiableListView get items => UnmodifiableListView(_mountList);
|
UnmodifiableListView get items => UnmodifiableListView(_mountList);
|
||||||
|
|
||||||
void _sort(list) {
|
|
||||||
list.sort((a, b) {
|
|
||||||
final res = a.type.compareTo(b.type);
|
|
||||||
if (res != 0) {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.name.compareTo(b.name);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _fetch() async {
|
Future<void> _fetch() async {
|
||||||
final response = await http.get(
|
final response = await http.get(
|
||||||
Uri.parse('${Uri.base.origin}/api/v1/mount_list'),
|
Uri.parse('${Uri.base.origin}/api/v1/mount_list'),
|
||||||
@ -34,8 +23,7 @@ class MountList with ChangeNotifier {
|
|||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
List<MountConfig> nextList = [];
|
List<MountConfig> nextList = [];
|
||||||
|
|
||||||
var data = jsonDecode(response.body);
|
jsonDecode(response.body).forEach((key, value) {
|
||||||
data.forEach((key, value) {
|
|
||||||
nextList.addAll(
|
nextList.addAll(
|
||||||
value.map((name) => MountConfig.fromJson(key, name)).toList(),
|
value.map((name) => MountConfig.fromJson(key, name)).toList(),
|
||||||
);
|
);
|
||||||
@ -48,6 +36,17 @@ class MountList with ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _sort(list) {
|
||||||
|
list.sort((a, b) {
|
||||||
|
final res = a.type.compareTo(b.type);
|
||||||
|
if (res != 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a.name.compareTo(b.name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void add(MountConfig config) {
|
void add(MountConfig config) {
|
||||||
var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name);
|
var item = _mountList.firstWhereOrNull((cfg) => cfg.name == config.name);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
@ -62,5 +61,7 @@ class MountList with ChangeNotifier {
|
|||||||
|
|
||||||
void remove(String name) {
|
void remove(String name) {
|
||||||
_mountList.removeWhere((item) => item.name == name);
|
_mountList.removeWhere((item) => item.name == name);
|
||||||
|
|
||||||
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:repertory/models/mount.dart';
|
||||||
import 'package:repertory/models/mount_list.dart';
|
import 'package:repertory/models/mount_list.dart';
|
||||||
import 'package:repertory/widgets/mount_widget.dart';
|
import 'package:repertory/widgets/mount_widget.dart';
|
||||||
|
|
||||||
@ -17,7 +18,10 @@ class _MountListWidgetState extends State<MountListWidget> {
|
|||||||
builder: (context, mountList, widget) {
|
builder: (context, mountList, widget) {
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemBuilder: (context, idx) {
|
itemBuilder: (context, idx) {
|
||||||
return MountWidget(mountConfig: mountList.items[idx]);
|
return ChangeNotifierProvider(
|
||||||
|
create: (context) => Mount(mountList.items[idx]),
|
||||||
|
child: const MountWidget(),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
itemCount: mountList.items.length,
|
itemCount: mountList.items.length,
|
||||||
);
|
);
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:repertory/types/mount_config.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:repertory/models/mount.dart';
|
||||||
|
|
||||||
class MountWidget extends StatelessWidget {
|
class MountWidget extends StatelessWidget {
|
||||||
final MountConfig mountConfig;
|
const MountWidget({super.key});
|
||||||
const MountWidget({super.key, required this.mountConfig});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -11,7 +11,11 @@ class MountWidget extends StatelessWidget {
|
|||||||
child: Container(
|
child: Container(
|
||||||
height: 40,
|
height: 40,
|
||||||
color: Colors.blue,
|
color: Colors.blue,
|
||||||
child: Text('${mountConfig.type} ${mountConfig.name}'),
|
child: Consumer<Mount>(
|
||||||
|
builder: (context, mount, widget) {
|
||||||
|
return Text('${mount.type} ${mount.name}');
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user