Flutter自定义组件插件fc_components的使用

Flutter自定义组件插件fc_components的使用

中文

flutter自定义组件

我的常用自定义组件集合,同时也欢迎大家添加自己的。


安装

pubspec.yaml 文件中添加以下依赖:

dependencies:
  fc_components: ^0.5.2

然后运行以下命令以更新依赖:

flutter pub get

使用

引入你想要使用的组件,例如 FcButton

import 'package:fc_components/fc_button.dart';

// 在你的widget中使用
[@override](/user/override)
Widget build(BuildContext context) {
  return FcButton(
    text: '点击我',
    onPressed: () {
      print('按钮被点击了');
    },
  );
}

组件列表

以下是 fc_components 插件支持的组件及其说明:

名称 说明 加入版本
FcCell 单元格 0.1.0
FcButton 按钮 0.2.0
FcEmpty 空状态 0.3.0
FcSortButton 排序按钮 0.4.0
FcNavigationBar 底部导航 0.5.0

示例代码

以下是一个完整的示例代码,展示如何使用 fc_components 插件中的组件。

example/lib/main.dart

import 'package:example/cell.dart';
import 'package:example/sort_button.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FC Components Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'FC Components Example'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: [
          // 使用 FcCell
          Card(
            child: ListTile(
              title: const Text('FcCell'),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return const Cell();
                }));
              },
            ),
          ),

          // 使用 FcButton(未完成)
          Card(
            child: ListTile(
              title: const Text('FcButton(未完工)'),
              onTap: () {
                // Navigator.push(context, MaterialPageRoute(builder: (context) {
                //   return const Button();
                // }));
              },
            ),
          ),

          // 使用 FcEmpty(未完成)
          Card(
            child: ListTile(
              title: const Text('FcEmpty(未完工)'),
              onTap: () {
                // Navigator.push(context, MaterialPageRoute(builder: (context) {
                //   return const Cell();
                // }));
              },
            ),
          ),

          // 使用 FcSortButton
          Card(
            child: ListTile(
              title: const Text('FcSortButton'),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return const SortButton();
                }));
              },
            ),
          ),

          // 使用 FcNavigationBar
          Card(
            child: ListTile(
              title: const Text('FcNavigationBar'),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return NavigationBar(
                    destinations: const [],
                  );
                }));
              },
            ),
          ),
        ],
      ),
    );
  }
}

更多关于Flutter自定义组件插件fc_components的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自定义组件插件fc_components的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


fc_components 是一个用于 Flutter 的自定义组件插件,它提供了一些常用的 UI 组件和工具,帮助开发者快速构建应用程序。以下是如何使用 fc_components 插件的基本步骤。

1. 安装插件

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

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

然后,运行以下命令来安装依赖:

flutter pub get

2. 导入插件

在你的 Dart 文件中导入 fc_components

import 'package:fc_components/fc_components.dart';

3. 使用组件

fc_components 提供了多种自定义组件,你可以根据自己的需求使用它们。以下是一些常见组件的使用示例。

3.1. CustomButton

CustomButton 是一个自定义按钮组件,支持多种样式和点击事件。

CustomButton(
  text: 'Click Me',
  onPressed: () {
    print('Button clicked!');
  },
  style: CustomButtonStyle.primary,
);

3.2. CustomTextField

CustomTextField 是一个自定义文本输入框,支持提示文本、输入校验等功能。

CustomTextField(
  hintText: 'Enter your name',
  onChanged: (value) {
    print('Input changed: $value');
  },
  validator: (value) {
    if (value == null || value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  },
);

3.3. CustomCard

CustomCard 是一个自定义卡片组件,支持阴影、圆角等样式。

CustomCard(
  child: Text('This is a custom card'),
  elevation: 5,
  borderRadius: BorderRadius.circular(10),
);

4. 自定义主题

fc_components 还支持自定义主题,你可以通过 CustomTheme 来设置全局样式。

void main() {
  runApp(
    MaterialApp(
      theme: CustomTheme.lightTheme,
      home: MyHomePage(),
    ),
  );
}

5. 其他组件

fc_components 还提供了其他一些组件,如 CustomDialog, CustomLoader, CustomAppBar 等,你可以根据需要使用它们。

6. 示例代码

以下是一个简单的示例,展示了如何使用 fc_components 中的一些组件。

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: CustomTheme.lightTheme,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: Text('FC Components Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CustomButton(
              text: 'Click Me',
              onPressed: () {
                print('Button clicked!');
              },
              style: CustomButtonStyle.primary,
            ),
            SizedBox(height: 20),
            CustomTextField(
              hintText: 'Enter your name',
              onChanged: (value) {
                print('Input changed: $value');
              },
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return 'Please enter some text';
                }
                return null;
              },
            ),
            SizedBox(height: 20),
            CustomCard(
              child: Text('This is a custom card'),
              elevation: 5,
              borderRadius: BorderRadius.circular(10),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部