Flutter全屏控制插件flutter_fullscreen的使用

发布于 1周前 作者 songsunli 来自 Flutter

Flutter全屏控制插件flutter_fullscreen的使用

flutter_fullscreen 是一个简单的包,允许在Flutter的所有平台上设置全屏模式。支持的平台包括Web、Linux、macOS、Windows、Android和iOS。

特性

  • 支持以下平台的全屏模式:
    • Web
    • Linux
    • macOS
    • Windows
    • Android
    • iOS

开始使用

安装

在你的 pubspec.yaml 文件中添加依赖:

dependencies:
  flutter_fullscreen: ^1.1.0

使用方法

初始化

确保在 main 函数中添加以下两行代码:

import 'package:flutter_fullscreen/flutter_fullscreen.dart';

void main() async {
    // 确保这两行代码被添加到main函数中
    WidgetsFlutterBinding.ensureInitialized();
    await FullScreen.ensureInitialized();

    runApp(const MyApp());
}

设置全屏

你可以通过调用 FullScreen.setFullScreen(true) 来启用全屏模式,或者调用 FullScreen.setFullScreen(false) 来退出全屏模式。

// 启用全屏
FullScreen.setFullScreen(true);

// 退出全屏
FullScreen.setFullScreen(false);

监听全屏状态变化

你可以监听全屏状态的变化,并根据需要更新UI。

import 'package:flutter/material.dart';
import 'package:flutter/src/services/system_chrome.dart';
import 'package:flutter_fullscreen/flutter_fullscreen.dart';

class Page extends StatefulWidget {
  const Page({super.key});

  [@override](/user/override)
  State<Page> createState() => _PageState();
}

class _PageState extends State<Page> with FullScreenListener {
  bool isFullScreen = FullScreen.isFullScreen;

  [@override](/user/override)
  void initState() {
    FullScreen.addListener(this);
    super.initState();
  }

  [@override](/user/override)
  void dispose() {
    FullScreen.removeListener(this);
    super.dispose();
  }

  [@override](/user/override)
  void onFullScreenChanged(bool enabled, SystemUiMode? systemUiMode) {
    setState(() {
      isFullScreen = enabled;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(isFullScreen ? "Fullscreen" : "Not Fullscreen"),
      ),
      body: Center(
        child: IconButton(
          onPressed: () {
            FullScreen.setFullScreen(!isFullScreen);
          },
          icon: Icon(isFullScreen ? Icons.fullscreen_exit : Icons.fullscreen),
        ),
      ),
    );
  }
}

示例Demo

以下是一个完整的示例项目,展示了如何使用 flutter_fullscreen 插件来切换应用的全屏状态。

/*
flutter_fullscreen

Copyright (c) 2024 Jefferey Neuffer <jeff@jefferey.dev>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

import 'package:flutter/material.dart';
import 'package:flutter_fullscreen/flutter_fullscreen.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FullScreen.ensureInitialized();
  runApp(const App());
}

class App extends StatefulWidget {
  const App({super.key});

  [@override](/user/override)
  State<App> createState() => _AppState();
}

class _AppState extends State<App> with FullScreenListener {
  bool isFullScreen = FullScreen.isFullScreen;

  [@override](/user/override)
  void initState() {
    FullScreen.addListener(this);
    super.initState();
  }

  [@override](/user/override)
  void dispose() {
    FullScreen.removeListener(this);
    super.dispose();
  }

  [@override](/user/override)
  void onFullScreenChanged(bool enabled, SystemUiMode? systemUiMode) {
    setState(() {
      isFullScreen = enabled;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Fullscreen Example",
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Row(
            children: [
              Text("Fullscreen Example"),
            ],
          ),
        ),
        body: Center(
          child: IconButton(
            onPressed: () {
              FullScreen.setFullScreen(!isFullScreen);
            },
            icon: Icon(isFullScreen ? Icons.fullscreen_exit : Icons.fullscreen),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter全屏控制插件flutter_fullscreen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter全屏控制插件flutter_fullscreen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用flutter_fullscreen插件来控制全屏模式的示例代码。这个插件允许你在iOS和Android设备上轻松切换全屏模式。

首先,确保你已经在pubspec.yaml文件中添加了flutter_fullscreen依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_fullscreen: ^1.3.2  # 请检查最新版本号

然后,运行flutter pub get来安装依赖。

接下来,在你的Flutter项目中,你可以按照以下步骤使用flutter_fullscreen插件:

  1. 导入插件

在你的Dart文件中导入flutter_fullscreen插件:

import 'package:flutter_fullscreen/flutter_fullscreen.dart';
  1. 初始化插件

在你的主应用入口(通常是main.dart)中,你可以初始化FlutterFullscreen实例。通常在MaterialAppCupertinoApp的builder中设置全屏监听器。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FlutterFullscreen(
      child: MaterialApp(
        title: 'Flutter Fullscreen Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(),
        builder: (context, child) {
          return FullscreenOverlay(
            child: child,
            // 可选:监听全屏状态变化
            onFullScreenStateChanged: (isFullScreen) {
              print('Is Full Screen: $isFullScreen');
            },
          );
        },
      ),
    );
  }
}
  1. 控制全屏状态

在你的页面或组件中,你可以使用FlutterFullscreen实例的方法来切换全屏状态。例如,在按钮点击事件中:

import 'package:flutter/material.dart';
import 'package:flutter_fullscreen/flutter_fullscreen.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Fullscreen Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () async {
                bool isFullScreen = await FlutterFullscreen.isFullScreen;
                await FlutterFullscreen.toggleFullScreen();
                print('Switched to ${!isFullScreen ? 'Full' : 'Not Full'} Screen');
              },
              child: Text('Toggle Full Screen'),
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,当用户点击按钮时,应用会在全屏和非全屏模式之间切换,并且会在控制台中打印当前的全屏状态。

这个示例代码展示了如何在Flutter项目中使用flutter_fullscreen插件来控制全屏模式。根据你的具体需求,你可以进一步自定义和扩展这个功能。

回到顶部