flutter_smartconfig/example/lib/main.dart
2019-01-27 19:34:09 +01:00

188 lines
5.7 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_smartconfig/flutter_smartconfig.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ESP Onetouch Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'ESP Onetouch Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _bssidFilter = new TextEditingController();
final TextEditingController _ssidFilter = new TextEditingController();
final TextEditingController _passwordFilter = new TextEditingController();
bool _isLoading = false;
String _ssid = "";
String _bssid = "";
String _password = "";
_MyHomePageState() {
_ssidFilter.addListener(_ssidListen);
_passwordFilter.addListener(_passwordListen);
_bssidFilter.addListener(_bssidListen);
}
void _ssidListen() {
if (_ssidFilter.text.isEmpty) {
_ssid = "";
} else {
_ssid = _ssidFilter.text;
}
}
void _bssidListen() {
if (_bssidFilter.text.isEmpty) {
_bssid = "";
} else {
_bssid = _bssidFilter.text;
}
}
void _passwordListen() {
if (_passwordFilter.text.isEmpty) {
_password = "";
} else {
_password = _passwordFilter.text;
}
}
Future<void> _configureEsp() async {
setState(() {
_isLoading = true;
});
dynamic data = await FlutterSmartconfig.configureEsp(
ssid: _ssid, bssid: _bssid, password: _password);
print(data);
setState(() {
_isLoading = false;
});
}
Future<void> _getConnectedWiFiInfo() async {
Map<PermissionGroup, PermissionStatus> permissions =
await PermissionHandler().requestPermissions([
PermissionGroup.location,
PermissionGroup.locationAlways,
PermissionGroup.locationWhenInUse
]);
if (permissions[PermissionGroup.location] != PermissionStatus.granted ||
permissions[PermissionGroup.locationAlways] !=
PermissionStatus.granted ||
permissions[PermissionGroup.locationWhenInUse] !=
PermissionStatus.granted) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("Fehler!"),
content: new Text(
"Wir benötigen Zugriff auf deinen Standort um auf die WLAN Funktion deines Betriessystems zugreifen zu können!"),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
return;
}
Map<String, String> data = await FlutterSmartconfig.getConnectedWiFiInfo();
setState(() {
_ssidFilter.text = data["ssid"];
_bssidFilter.text = data["bssid"];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _isLoading
? Container(
child: Center(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.lightBlue),
),
),
color: Colors.white.withOpacity(0.8),
)
: new Container(
padding: new EdgeInsets.all(10.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(height: 10),
new Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text("ESP Touch v0.3.7.0"),
new TextField(
controller: _ssidFilter,
decoration:
new InputDecoration(labelText: 'ssid'),
),
new TextField(
controller: _bssidFilter,
decoration:
new InputDecoration(labelText: 'bssid'),
),
RaisedButton(
child: Text('Get Connected WiFi details'),
onPressed: _getConnectedWiFiInfo,
)
])),
new Container(
child: new TextField(
controller: _passwordFilter,
decoration:
new InputDecoration(labelText: 'Password'),
),
),
new RaisedButton(
child: new Text('Configure ESP'),
onPressed: _configureEsp,
),
],
))) // This trailing comma makes auto-formatting nicer for build methods.
);
}
}