flutter_auto_orientation 1.0.0-beta.1
flutter_auto_orientation: ^1.0.0-beta.1 copied to clipboard
A Flutter plugin for controlling screen orientation, supporting both Android and iOS platforms.一个用于控制屏幕方向的Flutter插件,支持Android和iOS平台。
// A complete example app to demonstrate the usage of the flutter_auto_orientation plugin.
// 一个完整的示例应用,用于展示 flutter_auto_orientation 插件的用法。
import 'package:flutter/material.dart';
import 'package:flutter_auto_orientation/flutter_auto_orientation.dart';
// The main entry point of the application.
// 应用的主入口。
void main() {
// Ensure that Flutter is initialized and the services are ready.
// 确保 Flutter 初始化并且服务已准备就绪。
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
// The root widget of the application.
// 应用的根 Widget。
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
// The title of the application.
// 应用的标题。
title: 'Auto Orientation Demo',
// The theme of the application.
// 应用的主题。
theme: ThemeData(primarySwatch: Colors.blue),
// The home screen of the application.
// 应用的主屏幕。
home: const MyHomePage(title: '自动方向 Demo'),
);
}
}
// The home page with buttons to control the orientation.
// 带有控制方向按钮的主页。
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// The title of the home page.
// 主页的标题。
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// A variable to store the text of the currently active button.
// 用于存储当前处于激活状态的按钮文本。
String _currentMode = '无';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// The title in the app bar.
// 应用栏中的标题。
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// Button to set orientation to landscape right.
// 设置为横屏(右旋)的按钮。
_buildButton('横屏 (右旋)', '横屏 (右旋)', () => FlutterAutoOrientation.landscapeRightMode()),
// Button to set orientation to landscape left.
// 设置为横屏(左旋)的按钮。
_buildButton('横屏 (左旋)', '横屏 (左旋)', () => FlutterAutoOrientation.landscapeLeftMode()),
// Button to set orientation to portrait up.
// 设置为竖屏(正向)的按钮。
_buildButton('竖屏 (正向)', '竖屏 (正向)', () => FlutterAutoOrientation.portraitUpMode()),
// Button to set orientation to portrait down.
// 设置为竖屏(反向)的按钮。
_buildButton('竖屏 (反向)', '竖屏 (反向)', () => FlutterAutoOrientation.portraitDownMode()),
// Button to set orientation to auto portrait with sensor.
// 设置为竖屏自动模式(带传感器)的按钮。
_buildButton(
'竖屏自动 (带传感器)',
'竖屏自动 (带传感器)',
() => FlutterAutoOrientation.portraitAutoMode(forceSensor: true),
),
// Button to set orientation to auto landscape with sensor.
// 设置为横屏自动模式(带传感器)的按钮。
_buildButton(
'横屏自动 (带传感器)',
'横屏自动 (带传感器)',
() => FlutterAutoOrientation.landscapeAutoMode(forceSensor: true),
),
// Button to set orientation to full auto with sensor.
// 设置为全自动模式(带传感器)的按钮。
_buildButton('全自动 (带传感器)', '全自动 (带传感器)', () => FlutterAutoOrientation.fullAutoMode(forceSensor: true)),
// Button to set orientation to user setting.
// 设置为根据用户配置旋转的按钮。
_buildButton('根据用户配置旋转', '根据用户配置旋转', () => FlutterAutoOrientation.setScreenOrientationUser()),
],
),
),
),
),
);
}
// A helper function to build a styled button.
// 一个用于构建样式化按钮的辅助函数。
Widget _buildButton(String buttonText, String currentMode, VoidCallback onPressed) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
// Change button color to green if it's the current mode, otherwise use blue.
// 如果按钮是当前模式,则将颜色更改为绿色,否则使用蓝色。
foregroundColor: Colors.white,
backgroundColor: buttonText == _currentMode ? Colors.green : Colors.blue,
// Minimum size of the button.
// 按钮的最小尺寸。
minimumSize: const Size.fromHeight(50),
// Shape and border radius of the button.
// 按钮的形状和边框半径。
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
// Padding of the button.
// 按钮的内边距。
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
),
onPressed: () {
// Update the state to reflect the new active mode.
// 更新状态以反映新的激活模式。
setState(() {
_currentMode = buttonText;
});
// Execute the original onPressed callback.
// 执行原始的 onPressed 回调。
onPressed();
},
child: Text(buttonText, style: const TextStyle(fontSize: 16)),
),
);
}
}