Flutter自动调整大小插件auto_size_widget的使用

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

Flutter自动调整大小插件auto_size_widget的使用

auto_size_widget 是一个Flutter插件,允许用户通过拖动小部件的角来调整子小部件的大小。以下是该插件的详细使用说明和示例代码。

安装

  1. 将最新版本的包添加到您的 pubspec.yaml 文件中(并运行 dart pub get):

    dependencies:
        auto_size_widget: ^0.0.4
    
  2. 导入包并在您的Flutter应用中使用它:

    import 'package:auto_size_widget/auto_size_widget.dart';
    

示例

1. 调整宽度和高度

为了同时调整宽度和高度,initialWidthinitialHeight 应与 maxWidthmaxHeight 分别不同。

AutoSizeWidget(
  initialWidth: 120,
  initialHeight: 120,
  maxWidth: 320,
  maxHeight: 320,
  boxDecoration: BoxDecoration(
    border: Border.all(width: 1, color: Colors.grey),
    borderRadius: BorderRadius.circular(5)),
  child: Image.network(
    'https://docs.flutter.dev/assets/images/dash/early-dash-sketches2.jpg',
    fit: BoxFit.fill,
  ),
)

2. 仅调整高度

为了确保宽度不变,initialWidthmaxWidth 应相同。

AutoSizeWidget(
  initialWidth: 250,
  initialHeight: 120,
  maxWidth: 250,
  maxHeight: 280,
  boxDecoration: BoxDecoration(
    border: Border.all(width: 1, color: Colors.grey),
    borderRadius: BorderRadius.circular(5),
  ),
  child: const SelectableText(
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
  ),
)           

3. 仅调整宽度

为了确保高度不变,initialHeightmaxHeight 应相同。

AutoSizeWidget(
  initialWidth: 150,
  initialHeight: 200,
  maxWidth: 300,
  maxHeight: 200,
  boxDecoration: BoxDecoration(
    border: Border.all(width: 1, color: Colors.grey),
    borderRadius: BorderRadius.circular(5)),
  child: TextFormField(
    keyboardType: TextInputType.multiline,
    maxLines: 3 * 120,
    textAlign: TextAlign.start,
    decoration: const InputDecoration(
      focusedBorder: InputBorder.none,
      contentPadding:
        EdgeInsets.symmetric(vertical: 11, horizontal: 15),
      hintText: "Enter text here"
    ),
  ),
)                  

完整示例 Demo

以下是一个完整的示例应用,展示了如何在实际应用中使用 AutoSizeWidget

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Auto Size Widget Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const AutoSizeWidgetExample(),
    );
  }
}

class AutoSizeWidgetExample extends StatelessWidget {
  const AutoSizeWidgetExample({Key? key});

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
            backgroundColor: Colors.blue[300],
            centerTitle: false,
            title: const Text('Auto Size Widget Example')),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.all(2.0),
                  child: AutoSizeWidget(
                    initialWidth: 120,
                    initialHeight: 80,
                    maxWidth: 320,
                    boxDecoration: BoxDecoration(
                        border: Border.all(width: 1, color: Colors.grey),
                        borderRadius: BorderRadius.circular(5)),
                    maxHeight: 280,
                    showIcon: false,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Image.network(
                        'https://docs.flutter.dev/assets/images/dash/BigDashAndLittleDash.png',
                        fit: BoxFit.fill,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(2.0),
                  child: AutoSizeWidget(
                    initialWidth: 180,
                    initialHeight: 150,
                    maxWidth: 300,
                    maxHeight: 150,
                    boxDecoration: BoxDecoration(
                        border: Border.all(width: 1, color: Colors.grey),
                        borderRadius: BorderRadius.circular(5)),
                    showIcon: true,
                    child: const TextField(
                      keyboardType: TextInputType.multiline,
                      maxLines: 3 * 120,
                      textAlign: TextAlign.start,
                      decoration: InputDecoration(
                          focusedBorder: InputBorder.none,
                          contentPadding: EdgeInsets.symmetric(
                              vertical: 11, horizontal: 15),
                          hintText: "Enter text here"),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: AutoSizeWidget(
                    initialWidth: 250,
                    initialHeight: 120,
                    maxWidth: 250,
                    boxDecoration: BoxDecoration(
                        border: Border.all(width: 1, color: Colors.grey),
                        borderRadius: BorderRadius.circular(5)),
                    maxHeight: 280,
                    showIcon: true,
                    child: const Text(
                        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

通过以上示例,您可以快速上手 auto_size_widget 插件,并根据需求灵活调整小部件的大小。希望这对您有所帮助!


更多关于Flutter自动调整大小插件auto_size_widget的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自动调整大小插件auto_size_widget的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter中使用auto_size_widget插件的示例代码。这个插件允许你根据内容自动调整小部件的大小。

首先,你需要在你的pubspec.yaml文件中添加auto_size_text依赖(注意:auto_size_widget实际上可能指的是auto_size_text,因为auto_size_widget并不是一个常见的Flutter包名,而auto_size_text是一个非常流行的用于自动调整文本大小的包)。

dependencies:
  flutter:
    sdk: flutter
  auto_size_text: ^3.0.0 # 请确保使用最新版本

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

接下来,你可以在你的Flutter应用中使用AutoSizeText小部件。以下是一个简单的示例:

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

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

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

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AutoSizeText Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'This is a fixed size Text widget.',
              style: TextStyle(fontSize: 24),
            ),
            SizedBox(height: 20),
            AutoSizeText(
              'This is an AutoSizeText widget that will resize based on the available space.',
              style: TextStyle(fontSize: 24),
              maxLines: 2, // 限制最大行数
              minFontSize: 12, // 最小字体大小
              maxFontSize: 24, // 最大字体大小
            ),
          ],
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含两个文本小部件:一个是固定大小的Text小部件,另一个是AutoSizeText小部件。AutoSizeText小部件会根据可用的空间自动调整其大小,这在不同的屏幕尺寸或方向变化时特别有用。

AutoSizeText的主要参数包括:

  • style:文本的样式。
  • maxLines:文本的最大行数。
  • minFontSize:文本的最小字体大小。
  • maxFontSize:文本的最大字体大小。

你可以根据需要调整这些参数以满足你的应用需求。希望这个示例能帮助你理解如何在Flutter中使用auto_size_text插件!

回到顶部