Skip to content

Instantly share code, notes, and snippets.

@namnv609
Created November 15, 2019 07:30
Show Gist options
  • Save namnv609/566afeebbe9a9f84b61d53281e7be947 to your computer and use it in GitHub Desktop.
Save namnv609/566afeebbe9a9f84b61d53281e7be947 to your computer and use it in GitHub Desktop.
[Flutter][Simple app] hosts management GUI
import 'package:flutter/material.dart';
class IpDetail extends StatelessWidget {
final String ipAddr;
final List<String> ipDomains;
IpDetail(this.ipAddr, this.ipDomains);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('IP Detail: ${this.ipAddr}'),
),
body: Container(
child: ListView.builder(
itemCount: this.ipDomains.length ?? 0,
itemBuilder: (BuildContext context, idx) {
return Card(
child: ListTile(
title: Text(this.ipDomains[idx]),
),
);
},
),
)
);
}
}
import 'dart:io';
import 'package:example_flutter/ip_detail.dart';
import 'package:flutter/foundation.dart' show debugDefaultTargetPlatformOverride;
import 'package:flutter/material.dart';
void main() async {
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
runApp(HostsGUI());
}
class HostsGUI extends StatefulWidget {
@override
HostsGUIState createState() => HostsGUIState();
}
class HostsGUIState extends State<HostsGUI> {
List<String> hostsLines = [];
List<String> hostsIPs = [];
Map<String, List<String>> ipDomainMap = {};
getHostsContent() async {
try {
File file = File('/etc/hosts');
String fileContent = await file.readAsString();
fileContent.split('\n').forEach((line) {
line = line.trim();
RegExp commentLineRegExp = RegExp(r'^\#.+$');
if (!commentLineRegExp.hasMatch(line) && line != r'\n' && line != null && line != '') {
hostsLines.add(line.replaceAll(r'\s+', ' ').trim());
}
});
hostsLines.forEach((hostsLine) {
List<String> hostInfo = hostsLine.split(' ');
String ipAddr = hostInfo?.first;
var domainObj = ipDomainMap[ipAddr] ?? [];
hostInfo?.skip(1)?.forEach((domain) {
domainObj.add(domain);
});
ipDomainMap[ipAddr] = domainObj;
hostsIPs.add(hostInfo?.first);
});
setState(() {
hostsIPs = hostsIPs.toSet().toList();
ipDomainMap = ipDomainMap;
});
} on Exception catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
getHostsContent();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'hosts Management GUI',
home: Scaffold(
appBar: AppBar(
title: Text('IP Addresses'),
actions: <Widget>[
FlatButton(
child: Icon(Icons.save),
),
FlatButton(
child: Icon(Icons.exit_to_app),
)
],
),
body: Container(
child: ListView.builder(
itemCount: hostsIPs.length,
itemBuilder: (BuildContext context, int index) {
String ipAddr = hostsIPs[index];
List<String> ipDomains = ipDomainMap[ipAddr];
return Card(
child: ListTile(
title: Text(ipAddr),
trailing: Icon(Icons.chevron_right),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => IpDetail(ipAddr, ipDomains))
);
},
),
);
},
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment