0

有没有办法使用 Flutter 检查设备屏幕是否是圆形的?

经过一番搜索,我只发现如何使用本机代码:https://developer.android.com/reference/android/content/res/Configuration?hl=es#isScreenRound()

我实际上只需要它用于 Android,但我还没有找到使用 Flutter 的方法。我想使用此信息来更改应用程序布局。

4

1 回答 1

1

您将不得不使用平台渠道来做到这一点。

configureFlutterEngine()方法内部,创建了一个 MethodChannel 并调用setMethodCallHandler(). 然后检查实际方法并调用原生Android代码检查屏幕是否为圆角。

这是一些演示它的代码。

在您的MainActivity.kt中,您应该具有以下内容:

    package your.package.name
    
    import android.content.res.Configuration
    import androidx.annotation.NonNull
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugin.common.MethodChannel
    
    class MainActivity : FlutterActivity() {
        private val CHANNEL = "leonardosilva.com/screen-roundness"
    
        override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
            super.configureFlutterEngine(flutterEngine)
            MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call,
                                                                                                      result ->
                if (call.method == "getScreenRoundness") {
                    val config: Configuration = resources.configuration
    
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                        result.success(config.isScreenRound)
                    } else {
                        result.notImplemented()
                    }
                } else {
                    result.notImplemented()
                }
            }
        }
       
    }

在你的 Flutter 代码中,你需要一个变量来保存对平台通道的引用,所以在你Stateful WidgetState类中,你可以有这个:

    static const platform =
          const MethodChannel('leonardosilva.com/screen-roundness');

然后你可以有一个像下面这样的方法来调用本机端的方法:

    Future<void> _checkIfScreenIsRounded() async {
        try {
          final bool result = await platform.invokeMethod('getScreenRoundness');
          setState(() {
            _isScreenRound = result;
          });
        } on PlatformException catch (e) {
          print('Error $e');
        }
      }

这是一个示例,您可以粘贴到您的文件中main.dart并运行:

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      static const platform =
          const MethodChannel('leonardosilva.com/screen-roundness');
    
      bool _isScreenRound;
    
      bool _isLoading;
    
      @override
      void initState() {
        super.initState();
        _isLoading = false;
      }
    
      Future<void> _checkIfScreenIsRounded() async {
        try {
          final bool result = await platform.invokeMethod('getScreenRoundness');
          print(result);
          setState(() {
            _isScreenRound = result;
          });
        } on PlatformException catch (e) {
          print('Error $e');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: _isLoading
                ? CircularProgressIndicator()
                : Text(
                    _isScreenRound == null
                        ? 'Press button to check if screen is rounded'
                        : _isScreenRound
                            ? 'Screen is round'
                            : 'Screen is not round',
                  ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _checkIfScreenIsRounded,
            tooltip: 'Check if screen is round',
            child: Icon(Icons.add),
          ),
        );
      }
    }
于 2021-03-18T08:15:37.150 回答