Flutter自适应布局插件adaptive_keros的使用

Flutter自适应布局插件adaptive_keros的使用

Adaptive Keros 是一个用于创建适应不同操作系统的 Flutter 应用程序的包。通过使用这个包,你可以轻松地根据不同的设备(如 iPhone 和 Android)来调整你的应用布局。

示例截图

iPhone 15 Plus

Android

开始使用

要在你的 Flutter 项目中使用 adaptive_keros 包,首先需要在 pubspec.yaml 文件中添加依赖:

dependencies:
  adaptive_keros: ^1.0.1

然后运行 flutter pub get 来获取该包。

使用方法

在 Dart 文件中导入 adaptive_keros 包:

import 'package:adaptive_keros/adaptive_keros.dart';

接下来,你可以使用 Adaptive.scaffold 来构建一个自适应的 Scaffold。以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrangeAccent),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Adaptive.scaffold(
      string: 'Test',
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          // 创建一个自适应文本组件
          Adaptive.text(
            Colors.red[900],
            20,
            TextAlign.center,
            string: 'Ceci est notre App Test'
          ),
          
          // 创建一个自适应按钮组件
          Adaptive.button(
            onPressed: () => Adaptive.alert(
              context: context,
              onPressed: () {
                print("Test");
                Navigator.pop(context);
              },
            ),
            child: Adaptive.text(
              Colors.red[900],
              20,
              TextAlign.center,
              string: 'Press Me'
            ),
          ),
          
          // 创建另一个自适应按钮组件
          Adaptive.button(
            child: Adaptive.text(
              Colors.red[900],
              20,
              TextAlign.center,
              string: 'Montrer l\'alerte'
            ),
            onPressed: () {
              Adaptive.alert(
                context: context,
                onPressed: () {
                  Navigator.pop(context);
                }
              );
            }
          )
        ],
      )),
    );
  }
}

代码解释

  1. 导入包

    import 'package:adaptive_keros/adaptive_keros.dart';
    
  2. 创建应用入口

    void main() {
      runApp(const MyApp());
    }
    
  3. 定义 MyApp

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrangeAccent),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
  4. 定义 MyHomePage

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
  5. 实现 _MyHomePageState

    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Adaptive.scaffold(
          string: 'Test',
          body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Adaptive.text(
                    Colors.red[900],
                    20,
                    TextAlign.center,
                    string: 'Ceci est notre App Test'
                  ),
                  Adaptive.button(
                    onPressed: () => Adaptive.alert(
                      context: context,
                      onPressed: () {
                        print("Test");
                        Navigator.pop(context);
                      },
                    ),
                    child: Adaptive.text(
                      Colors.red[900],
                      20,
                      TextAlign.center,
                      string: 'Press Me'
                    ),
                  ),
                  Adaptive.button(
                    child: Adaptive.text(
                      Colors.red[900],
                      20,
                      TextAlign.center,
                      string: 'Montrer l\'alerte'
                    ),
                    onPressed: () {
                      Adaptive.alert(
                        context: context,
                        onPressed: () {
                          Navigator.pop(context);
                        }
                      );
                    }
                  )
                ],
              )),
        );
      }
    }
    

更多关于Flutter自适应布局插件adaptive_keros的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自适应布局插件adaptive_keros的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,adaptive_keros 是一个用于 Flutter 的自适应布局插件,它能够帮助开发者在不同的屏幕尺寸和方向上创建响应式布局。以下是如何在 Flutter 项目中使用 adaptive_keros 的一些示例代码。

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

dependencies:
  flutter:
    sdk: flutter
  adaptive_keros: ^最新版本号  # 请替换为当前最新版本号

然后运行 flutter pub get 以获取依赖。

示例代码

1. 导入必要的包

在你的 Dart 文件中导入 adaptive_keros 包:

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

2. 使用 AdaptiveScaffold

AdaptiveScaffoldadaptive_keros 提供的一个核心组件,用于创建自适应的页面布局。

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: AdaptiveScaffold(
        appBar: AppBar(
          title: Text('Adaptive Keros Demo'),
        ),
        body: Center(
          child: Text('Hello, Adaptive Keros!'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

3. 使用自适应间距和大小

adaptive_keros 提供了一些工具类来帮助你创建自适应的间距和大小。

class DemoPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Demo Page'),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(
          horizontal: Adaptive.h(16),  // 自适应水平间距
          vertical: Adaptive.v(16),    // 自适应垂直间距
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              'Adaptive Layout',
              style: TextStyle(fontSize: Adaptive.font(24)),  // 自适应字体大小
            ),
            SizedBox(height: Adaptive.v(8)),
            Text(
              'This is a demo of using adaptive_keros package.',
              style: TextStyle(fontSize: Adaptive.font(16)),
            ),
          ],
        ),
      ),
    );
  }
}

4. 使用 AdaptiveLayoutBuilder

AdaptiveLayoutBuilder 可以根据屏幕尺寸和方向创建不同的布局。

class ResponsiveLayoutPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Responsive Layout'),
      ),
      body: AdaptiveLayoutBuilder(
        smallScreen: (context, child) {
          return Column(
            children: <Widget>[
              child,
              child,
            ],
          );
        },
        mediumScreen: (context, child) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              child,
              child,
            ],
          );
        },
        largeScreen: (context, child) {
          return GridView.count(
            crossAxisCount: 2,
            children: List.generate(4, (index) => child),
          );
        },
        child: (context) => Container(
          color: Colors.blueGrey[100],
          padding: EdgeInsets.all(16),
          child: Center(child: Text('Responsive Child')),
        ),
      ),
    );
  }
}

总结

以上代码展示了如何在 Flutter 项目中使用 adaptive_keros 插件来创建自适应布局。通过使用 AdaptiveScaffold、自适应间距和大小工具类以及 AdaptiveLayoutBuilder,你可以轻松地创建响应式 UI,以适应不同的屏幕尺寸和方向。希望这些示例代码对你有所帮助!

回到顶部