Flutter多功能组件集合插件components_toolbox的使用

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

Flutter多功能组件集合插件components_toolbox的使用

标题

Flutter多功能组件集合插件components_toolbox的使用

内容

A basic Toolbox for Flutter components with AppDelegate to manage the light and dark theme.

The toolbox class has the following components:

  • Rounded Container: A container with rounded corners.
  • TextField: A text field with a label.
  • Rounded Button: A button with a label (and an icon).
  • Text Button: A button with a label.
  • Markdown Text: A text that can parse markdown.
  • Carousel: A carousel that can be embedded on any component.
  • Custom Navigation Bar: A custom navigation bar with a title and a back button.
  • Loader: A loader that can be embedded on any component.
  • Theme Switcher: A switcher that can change the theme of the app.
  • Floating Navigation Bar: A navigation bar that can float on the screen.
  • AutoText: A text that can be resized automatically.
  • Custom Colors: A class that can be used to set custom colors for the app.
  • AutoScaleAnimatedColumn: A column that can be animated and resized automatically.
  • SpacedColumn: A column that have a space between each child.

Getting Started To use this class, you need to add the following dependency in your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  components_toolbox: ^7.0.0+1

Then, you need to import the class in your file:

import 'package:components_toolbox/components_toolbox.dart';

Usage Rounded Container rounded_container.png

RoundedContainer(
  child: Text('Hello World!'),
),

TextField custom_text_field.png

const CustomTextField("Simple"),
const SizedBox(height: 16),
const CustomTextField(
    "Obscure",
    obscureText: true,
),

Rounded Button rounded_button.png

Center(
    child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
                RoundedButton(
                    text: 'Simple',
                    onPressed: () {},
                    width: MediaQuery.of(context).size.width / 2 - 32,
                ),
                RoundedButton(
                    text: 'Shadow',
                    onPressed: () {},
                    shadow: CustomShadows.regularCentered,
                    width: MediaQuery.of(context).size.width / 2 - 32,
                ),
            ],
        ),
    ),
),
const SizedBox(height: 16),
RoundedButtonWithIcons(
    Icons.save,
    text: "With Icon",
    onPressed: () {},
),
const SizedBox(height: 16),
Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
        Center(
            child: OutlinedRoundedButton(
                "Outlined",
                onPressed: () {},
                width: 150,
            ),
        ),
        CircleButton(
            icon: Icons.add,
            onPressed: () {},
        ),
        AutoTextButton(
            "TextButton",
            onPressed: () {},
        ),
    ],
),

Dependencies The project uses the following dependencies as specified in pubspec.yaml:

  • shared_preferences: Used to store the selected app theme (dark or light).

示例代码


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

1 回复

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


当然,components_toolbox 是一个假设的 Flutter 插件,用于集合多功能组件。在实际情况下,你可能需要查找特定的插件文档或使用现有的类似插件。不过,我可以为你展示一个如何在 Flutter 中使用自定义插件或集合组件的示例代码结构。

首先,假设我们有一个自定义的 Flutter 插件 components_toolbox,它包含多个多功能组件。以下是如何在 Flutter 项目中使用这个插件的示例代码。

1. 添加插件依赖

首先,在你的 pubspec.yaml 文件中添加插件依赖项(假设插件已经在 Pub 仓库中):

dependencies:
  flutter:
    sdk: flutter
  components_toolbox: ^1.0.0  # 假设版本号为 1.0.0

2. 导入插件

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

import 'package:components_toolbox/components_toolbox.dart';

3. 使用插件中的组件

假设 components_toolbox 插件包含以下组件:

  • ToolboxButton:一个自定义按钮组件。
  • ToolboxTextField:一个自定义文本输入框组件。
  • ToolboxList:一个自定义列表组件。

下面是如何在你的 Flutter 应用中使用这些组件的示例代码:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Components Toolbox Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Components Toolbox Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            ToolboxButton(
              onPressed: () {
                print('ToolboxButton clicked!');
              },
              child: Text('Click Me'),
            ),
            SizedBox(height: 16),
            ToolboxTextField(
              controller: _controller,
              decoration: InputDecoration(
                labelText: 'Enter some text',
              ),
            ),
            SizedBox(height: 16),
            ToolboxList(
              items: ['Item 1', 'Item 2', 'Item 3'],
              onItemClick: (index) {
                print('Clicked on item: $index');
              },
            ),
          ],
        ),
      ),
    );
  }
}

4. 假设的插件代码(仅示例)

以下是一个假设的 components_toolbox 插件代码结构,用于展示如何定义这些组件:

// File: components_toolbox/lib/components_toolbox.dart

import 'package:flutter/material.dart';

// ToolboxButton Component
class ToolboxButton extends StatelessWidget {
  final VoidCallback onPressed;
  final Widget child;

  ToolboxButton({required this.onPressed, required this.child});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: onPressed,
      child: child,
      style: ButtonStyle(
        backgroundColor: MaterialStateProperty.all(Colors.blue),
      ),
    );
  }
}

// ToolboxTextField Component
class ToolboxTextField extends StatelessWidget {
  final TextEditingController controller;
  final InputDecoration decoration;

  ToolboxTextField({required this.controller, required this.decoration});

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: controller,
      decoration: decoration,
    );
  }
}

// ToolboxList Component
class ToolboxList extends StatelessWidget {
  final List<String> items;
  final ValueChanged<int> onItemClick;

  ToolboxList({required this.items, required this.onItemClick});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(items[index]),
          onTap: () => onItemClick(index),
        );
      },
    );
  }
}

请注意,上述 components_toolbox 插件代码是一个假设的示例,用于展示如何定义这些组件。在真实情况下,你需要根据你的实际插件文档来使用它。希望这个示例能帮助你理解如何在 Flutter 项目中使用自定义插件或集合组件。

回到顶部