Flutter圆角图片加文本背景插件rounded_image_with_textbg的使用

Flutter圆角图片加文本背景插件rounded_image_with_textbg的使用

简介

该插件用于获取圆角剪裁的用户图像,可以以多种方式使用:

  1. 如果网络图片无法加载,则可以显示带有随机颜色背景和用户名首字母的图像。
  2. 可以将用户图像更新为选定图标或其他小部件,类似于Gmail。
  3. 如果提供了有效的十六进制字符串uniqueId,则可以生成不会在刷新时改变的随机颜色。

特性

  1. 使用唯一的十六进制字符串生成背景的随机颜色。
  2. 如果网络图片未加载成功,则显示RoundedImageWithTextBG

开始使用

  1. 调用无状态小部件RoundedImageWithTextAndBG来创建一个具有文本背景的圆形图片。
  2. 使用getColorFromString()方法从字符串生成颜色。

完整示例

以下是一个完整的示例代码,展示了如何在Flutter应用中使用rounded_image_with_textbg插件。

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

// 示例数据模型
const textModel = [
  {'name': 'Alice', 'uniqueId': '12345'},
  {'name': 'Bob', 'uniqueId': '67890'},
  {'name': 'Charlie', 'uniqueId': 'abcde'}
];

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: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Home(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Rounded Image with Text Background'),
      ),
      body: ListView.builder(
        itemCount: textModel.length,
        itemBuilder: (BuildContext context, int index) => SizedBox(
          height: 60,
          child: ListItems(index: index),
        ),
      ),
    );
  }
}

class ListItems extends StatefulWidget {
  final int index;

  const ListItems({Key? key, required this.index}) : super(key: key);

  [@override](/user/override)
  State<ListItems> createState() => _ListItemsState();
}

class _ListItemsState extends State<ListItems> {
  bool isSelected = false;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return ListTile(
      onTap: () {
        setState(() {
          isSelected = !isSelected;
        });
      },
      title: Text(textModel[widget.index]['name']),
      leading: RoundedImageWithTextAndBG(
        radius: 20,
        isSelected: isSelected,
        uniqueId: textModel[widget.index]['uniqueId'],
        image: (widget.index % 3 == 0)
            ? 'https://picsum.photos/id/${widget.index}/800/800'
            : null,
        text: textModel[widget.index]['name'],
      ),
    );
  }
}

更多关于Flutter圆角图片加文本背景插件rounded_image_with_textbg的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter圆角图片加文本背景插件rounded_image_with_textbg的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


rounded_image_with_textbg 是一个用于在 Flutter 中创建带有圆角图片和文本背景的自定义 widget 的插件。以下是如何使用这个插件的详细步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  rounded_image_with_textbg: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

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

import 'package:rounded_image_with_textbg/rounded_image_with_textbg.dart';

3. 使用 RoundedImageWithTextBg Widget

你可以在你的 UI 中使用 RoundedImageWithTextBg widget 来创建一个带有圆角图片和文本背景的组件。

RoundedImageWithTextBg(
  imageUrl: 'https://example.com/image.jpg',  // 图片的URL
  borderRadius: 20.0,  // 圆角半径
  text: 'Sample Text',  // 显示的文本
  textBackgroundColor: Colors.black.withOpacity(0.5),  // 文本背景颜色
  textStyle: TextStyle(color: Colors.white, fontSize: 16),  // 文本样式
  width: 200,  // 图片宽度
  height: 150,  // 图片高度
  fit: BoxFit.cover,  // 图片的填充方式
)

4. 参数说明

  • imageUrl: 图片的 URL 或本地图片路径。
  • borderRadius: 图片的圆角半径。
  • text: 显示在图片上的文本。
  • textBackgroundColor: 文本背景的颜色,通常带有透明度。
  • textStyle: 文本的样式,如字体大小、颜色等。
  • width: 图片的宽度。
  • height: 图片的高度。
  • fit: 图片的填充方式,如 BoxFit.coverBoxFit.contain 等。

5. 示例代码

以下是一个完整的示例代码:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Rounded Image with Text Background'),
        ),
        body: Center(
          child: RoundedImageWithTextBg(
            imageUrl: 'https://via.placeholder.com/200',
            borderRadius: 20.0,
            text: 'Sample Text',
            textBackgroundColor: Colors.black.withOpacity(0.5),
            textStyle: TextStyle(color: Colors.white, fontSize: 16),
            width: 200,
            height: 150,
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
  }
}
回到顶部