At the beginning of the year, due to the business needs, I transferred to Flutter. Due to the limited time, I was basically in the state of learning and writing, and also made many mistakes. Here is a summary, mainly about the use of plug-ins, will be updated in the future

Checking network Status

Flutter plug-in connectivity

import 'package:connectivity/connectivity.dart';

checkConnect() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    returnconnectivityResult ! = ConnectivityResult.none }// Live monitor
var subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
    print(result);
});

Copy the code

Obtaining Device Information

The flutter plugin device_info obtains the device platform and unique DEVICE ID

import 'package:device_info/device_info.dart';

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isIOS) {
    // Obtain the iOS device ID
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    print(iosInfo.identifierForVendor);
} else if(Platform.isAndroid){
    // Obtain the Android device ID
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    print(androidInfo.androidId);
}

Copy the code

Global data sharingProvideThe use of

Introduced in pubspec.yaml file

The provider: ^ 3.1.0Copy the code

Define the global data structure data.dart

class DataModel with ChangeNotifier { String get deviceId => _deviceId; // Set the global device ID setDeviceId(id) {_deviceId = id; notifyListeners(); }}Copy the code

Read the corresponding data on the interface

Widget build(BuildContext context) {

    return WillPopScope(
    	// Block the return key
        onWillPop: () async {
          return false;
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text('flutter', textScaleFactor: 1,),
            backgroundColor: Colors.black,
          ),
          body: Consumer<DataModel>(
            builder: (context, DataModel model, child) => Stack(
              children: <Widget>[
                // Read global data provided
              	Text(model.deviceId)
              ]
            ),
          ),
        ));
        
  }
Copy the code

Set onWillPop to false to disable the device return key, which can be used in some business scenarios.

Modify global data provided


dataModel = Provider.of<DataModel>(context);
dataModel.setDeviceId('007');

Copy the code

The realization of camera scan code function

Flutter plug-in barcode_scan

import 'package:barcode_scan/barcode_scan.dart';

/ / code
Future scan() async {

  var options = ScanOptions(
    strings: {
      "cancel": 'cancel'."flash_on": 'flash_on'."flash_off": 'flash_off',});var result = await BarcodeScanner.scan(options: options);
  // Take the initiative to cancel without doing other processing
  if(result.type == ResultType.Cancelled) {
    return;
  }
  String barcode = result.rawContent;
  print(barcode);
  
}
Copy the code

Jump to the browser from the WebView

Flutter plug-in url_launcher

import 'package:url_launcher/url_launcher.dart';

Future<void> _launchInBrowser(String url) async {
  if (await canLaunch(url)) {
    await launch(
      url,
      forceSafariVC: false,
      forceWebView: false,
      headers: <String.String> {'my_header_key': 'my_header_value'}); }else {
    throw 'Could not launch $url'; }}Copy the code

Listening for keyboard events

Flutter plug-in flutter_keyboard_visibility

import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';

// Keyboard events
KeyboardVisibility.onChange.listen((bool visible) {
  print('Keyboard visibility update. Is visible: ${visible}');
  setState(() {
    showKeyboard = visible;
  });
});
Copy the code

Local data store

Flutter plug-in shared_preferences

// Set local data
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('account', account);

// Read local data
String account = prefs.get('account');
Copy the code