Flutter本地功能集合插件local_function_collections的使用

Flutter本地功能集合插件local_function_collections的使用

Local Function Collections 是一个简单的包,提供了导航、对话框和安全存储的功能。

特性

该包提供了以下功能:

  1. 导航功能,如跳转到、替换为和返回上一页。
  2. 对话框功能,如确认对话框、选项对话框和加载对话框。
  3. 安全存储功能,如写入、读取、移除和清除。

开始使用

确保你的 Flutter 版本是最新的。

使用方法

任何进一步的说明都已添加到 example.dart 文件中。

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

  [@override](/user/override)
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {

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

  onOKDialogPressed() => LocalDialogFunction.okDialog(
    context: context,
    contentText: "Hello Users!!!",
    contentAlign: TextAlign.justify,
  );

  onOptionDialogPressed() => LocalDialogFunction.optionDialog(
    context: context,
    contentText: "Do you like this package?",
    onAccept: () => LocalDialogFunction.okDialog(
      context: context,
      title: "Thank You!!!",
      contentText: "Thank you for appreciating our package!, soon there will be another new package launched.",
      contentAlign: TextAlign.justify,
    ),
    onDecline: () => LocalDialogFunction.okDialog(
      context: context,
      title: "We're Sorry",
      contentText: "We're sorry if our package isn't enough for you :(, for any critiques you can DM me on Instagram: @raznovrnf or my LinkedIn: Razy Firdana.",
      contentAlign: TextAlign.justify,
    ),
    contentAlign: TextAlign.justify,
  );

  onLoadingDialogPressed() {
    LocalDialogFunction.loadingDialog(
      context: context,
      contentText: "This loading will disappear in 3 second",
      contentAlign: TextAlign.justify,
    );

    Future.delayed(const Duration(seconds: 3), () {
      if(mounted) {
        LocalRouteNavigator.closeBack(context: context);
      }
    });
  }

  onMoveTo2ndPage() => LocalRouteNavigator.moveTo(
    context: context,
    target: const SecondPage(),
  );

  onMoveTo3rdPage() => LocalRouteNavigator.moveTo(
    context: context,
    target: const ThirdPage(
      isJumped: true,
    ),
  );

  onUpdateSecureStorage() async => await LocalSecureStorage.writeKey(
    key: "example",
    data: "This example was created at ${DateTime.now()}",
  );

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(
          "LCF Demo App 1st Page",
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(
          horizontal: 10.0,
        ),
        children: [
          SizedBox(
            height: MediaQuery.of(context).size.height / 4,
          ),
          Column(
            children: <Widget>[
              const Text(
                "欢迎来到第一页,\n让我们试用本地功能集合",
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 10.0,
              ),
              const Text(
                "对话框集合",
              ),
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () => onOKDialogPressed(),
                      child: const Padding(
                        padding: EdgeInsets.all(5.0),
                        child: Text(
                          "确定",
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () => onOptionDialogPressed(),
                      child: const Padding(
                        padding: EdgeInsets.all(5.0),
                        child: Text(
                          "选项",
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () => onLoadingDialogPressed(),
                      child: const Padding(
                        padding: EdgeInsets.all(5.0),
                        child: Text(
                          "加载",
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 5.0,
              ),
              const Text(
                "路由集合",
              ),
              Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () => onMoveTo2ndPage(),
                      child: const Padding(
                        padding: EdgeInsets.all(5.0),
                        child: Text(
                          "跳转到第2页",
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () => onMoveTo3rdPage(),
                      child: const Padding(
                        padding: EdgeInsets.all(5.0),
                        child: Text(
                          "跳转到第3页",
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 5.0,
              ),
              const Text(
                "安全存储集合",
              ),
              ElevatedButton(
                onPressed: () => onUpdateSecureStorage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "更新安全存储时间戳",
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

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

  [@override](/user/override)
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  String? storageResult;

  [@override](/user/override)
  void initState() {
    super.initState();

    onCheckSecureStorage();
  }

  onCheckSecureStorage() async {
    await LocalSecureStorage.readKey(key: "example").then((result) {
      setState(() {
        storageResult = result;
      });
    });
  }

  onCloseThisPage() => LocalRouteNavigator.closeBack(context: context);

  onReplaceTo3rdPage() => LocalRouteNavigator.replaceWith(
    context: context,
    target: const ThirdPage(
      isJumped: true,
    ),
  );

  onMoveTo3rdPage() => LocalRouteNavigator.moveTo(
    context: context,
    target: const ThirdPage(
      isJumped: false,
    ),
    callbackFunction: (_) => onCheckSecureStorage(),
  );

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(
          "LCF Demo App 第2页",
        ),
      ),
      body: ListView(
        children: [
          SizedBox(
            height: MediaQuery.of(context).size.height / 4,
          ),
          Column(
            children: <Widget>[
              const Text(
                "欢迎来到第2页...",
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 10.0,
              ),
              Text(
                "保存在本地安全存储中的文本: ${storageResult ?? "-"}",
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 10.0,
              ),
              ElevatedButton(
                onPressed: () => onCloseThisPage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "回到第1页",
                  ),
                ),
              ),
              const SizedBox(
                height: 5.0,
              ),
              ElevatedButton(
                onPressed: () => onMoveTo3rdPage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "跳转到第3页",
                  ),
                ),
              ),
              const SizedBox(
                height: 5.0,
              ),
              ElevatedButton(
                onPressed: () => onReplaceTo3rdPage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "用第3页替换当前页面",
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class ThirdPage extends StatefulWidget {
  final bool isJumped;

  const ThirdPage({
    super.key,
    required this.isJumped,
  });

  [@override](/user/override)
  State<ThirdPage> createState() => _ThirdPageState();
}

class _ThirdPageState extends State<ThirdPage> {
  String? storageResult;

  [@override](/user/override)
  void initState() {
    super.initState();

    onCheckSecureStorage();
  }

  onCheckSecureStorage() async {
    await LocalSecureStorage.readKey(key: "example").then((result) {
      setState(() {
        storageResult = result;
      });
    });
  }

  onCloseThisPage() => LocalRouteNavigator.closeBack(context: context);

  onRedirectThisPage() => LocalRouteNavigator.redirectTo(
    context: context,
    target: const FirstPage(),
  );

  onClearSecureStorage() async => await LocalSecureStorage.clearKey().then((_) => onCheckSecureStorage());

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text(
          "LCF Demo App 第3页",
        ),
      ),
      body: ListView(
        children: [
          SizedBox(
            height: MediaQuery.of(context).size.height / 4,
          ),
          Column(
            children: <Widget>[
              const Text(
                "欢迎来到第3页...",
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 10.0,
              ),
              Text(
                "保存在本地安全存储中的文本: ${storageResult ?? "-"}",
                textAlign: TextAlign.center,
              ),
              const SizedBox(
                height: 10.0,
              ),
              ElevatedButton(
                onPressed: () => onClearSecureStorage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "清除安全存储",
                  ),
                ),
              ),
              const SizedBox(
                height: 5.0,
              ),
              ElevatedButton(
                onPressed: () => onCloseThisPage(),
                child: Padding(
                  padding: const EdgeInsets.all(5.0),
                  child: Text(
                    widget.isJumped == true
                        ? "回到第1页"
                        : "回到第2页",
                  ),
                ),
              ),
              const SizedBox(
                height: 5.0,
              ),
              widget.isJumped == true ?
              const Material() :
              ElevatedButton(
                onPressed: () => onRedirectThisPage(),
                child: const Padding(
                  padding: EdgeInsets.all(5.0),
                  child: Text(
                    "重定向到第1页",
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

更多关于Flutter本地功能集合插件local_function_collections的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter本地功能集合插件local_function_collections的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


local_function_collections 是一个 Flutter 插件,它提供了一些本地功能的集合,方便开发者在 Flutter 应用中快速集成和使用。这个插件可能包含了一些常用的本地功能,比如文件操作、网络请求、设备信息获取等。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 local_function_collections 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  local_function_collections: ^1.0.0  # 请使用最新版本

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

使用插件

假设 local_function_collections 插件提供了以下几个功能:

  1. 文件操作
  2. 网络请求
  3. 设备信息获取

1. 文件操作

假设插件提供了一个 FileUtils 类来处理文件操作,你可以这样使用:

import 'package:local_function_collections/local_function_collections.dart';

void readFile() async {
  String content = await FileUtils.readFile('path/to/file.txt');
  print('File content: $content');
}

void writeFile() async {
  await FileUtils.writeFile('path/to/file.txt', 'Hello, World!');
}

2. 网络请求

假设插件提供了一个 NetworkUtils 类来处理网络请求,你可以这样使用:

import 'package:local_function_collections/local_function_collections.dart';

void fetchData() async {
  var response = await NetworkUtils.get('https://jsonplaceholder.typicode.com/posts');
  print('Response: $response');
}

3. 设备信息获取

假设插件提供了一个 DeviceInfo 类来获取设备信息,你可以这样使用:

import 'package:local_function_collections/local_function_collections.dart';

void getDeviceInfo() async {
  var deviceInfo = await DeviceInfo.getDeviceInfo();
  print('Device Info: $deviceInfo');
}

示例代码

以下是一个完整的示例,展示了如何使用 local_function_collections 插件中的功能:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Local Function Collections Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () async {
                  String content = await FileUtils.readFile('path/to/file.txt');
                  print('File content: $content');
                },
                child: Text('Read File'),
              ),
              ElevatedButton(
                onPressed: () async {
                  await FileUtils.writeFile('path/to/file.txt', 'Hello, World!');
                },
                child: Text('Write File'),
              ),
              ElevatedButton(
                onPressed: () async {
                  var response = await NetworkUtils.get('https://jsonplaceholder.typicode.com/posts');
                  print('Response: $response');
                },
                child: Text('Fetch Data'),
              ),
              ElevatedButton(
                onPressed: () async {
                  var deviceInfo = await DeviceInfo.getDeviceInfo();
                  print('Device Info: $deviceInfo');
                },
                child: Text('Get Device Info'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
回到顶部