Flutter界面更新高亮插件highlight_on_update的使用
Flutter界面更新高亮插件highlight_on_update的使用
highlight_on_update
https://pub.dev/packages/highlight_on_update
🔦 一个在文本更新时会被高亮显示的文本小部件。
示例
查看 示例代码.
开始使用
pub
在pubspec.yaml
文件中添加以下依赖:
dependencies:
highlight_on_update:
使用方法
在你的Flutter应用中使用HighlightOnUpdateText
小部件来实现高亮效果:
Row(
children: [
const Text(
'当前价格: $',
style: TextStyle(fontSize: 20),
),
HighlightOnUpdateText(
_priceText, // 这个文本会在更新时被高亮显示。
style: const TextStyle(fontSize: 20),
),
],
),
完整示例代码
以下是一个完整的示例代码,展示了如何使用highlight_on_update
插件。
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:highlight_on_update/highlight_on_update.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = TextEditingController();
late Timer timer;
String _customText = '编辑文本框以测试自定义值!';
String _priceText = '';
void _updatePrice(Timer _) {
setState(() {
_priceText = math.Random().nextInt(100000000).toString();
});
}
[@override](/user/override)
void initState() {
timer = Timer.periodic(
const Duration(seconds: 2),
_updatePrice,
);
super.initState();
}
[@override](/user/override)
void dispose() {
timer.cancel();
_controller.dispose();
super.dispose();
}
void _updateCustomText(String value) {
setState(() {
_customText = value;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return HighlightOnUpdateTheme(
to: Colors.white,
borderRadius: const BorderRadius.all(Radius.circular(4)),
child: GestureDetector(
onTap: FocusManager.instance.primaryFocus?.unfocus,
child: Scaffold(
appBar: AppBar(
title: Row(
children: [
const Text(
'当前价格: $',
style: TextStyle(fontSize: 20),
),
HighlightOnUpdateText(
_priceText,
style: const TextStyle(fontSize: 20),
),
],
),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'自定义文本:\n',
style: TextStyle(fontSize: 16),
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
HighlightOnUpdateText(
_customText,
style: const TextStyle(fontSize: 16),
to: const Color(0xAAFFFFFF),
),
],
),
// const Divider(height: 48.0),
const SizedBox(height: 12.0),
const Spacer(),
TextField(
controller: _controller,
minLines: 3,
maxLines: 3,
textInputAction: TextInputAction.newline,
onChanged: _updateCustomText,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
),
],
),
),
),
),
),
);
}
}
更多关于Flutter界面更新高亮插件highlight_on_update的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter界面更新高亮插件highlight_on_update的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
highlight_on_update
是一个 Flutter 插件,用于在界面更新时高亮显示变化的部件。这对于调试和优化 Flutter 应用的性能非常有用,因为它可以帮助你快速识别哪些部件在更新以及它们更新的频率。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 highlight_on_update
插件的依赖:
dependencies:
flutter:
sdk: flutter
highlight_on_update: ^0.1.0 # 请使用最新版本
然后,运行 flutter pub get
来安装插件。
使用插件
使用 highlight_on_update
插件非常简单。你只需要将你想要监控的部件包裹在 HighlightOnUpdate
部件中即可。
import 'package:flutter/material.dart';
import 'package:highlight_on_update/highlight_on_update.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Highlight on Update Example'),
),
body: Center(
child: HighlightOnUpdate(
child: MyWidget(),
),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Counter:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
);
}
}
解释
-
HighlightOnUpdate: 这是一个包裹部件,它会监控其子部件的更新。每当子部件更新时,它会在屏幕上显示一个高亮效果,通常是短暂的闪烁或边框变化。
-
MyWidget: 这是一个简单的有状态部件,包含一个计数器和一个按钮。每次点击按钮时,计数器会增加,并且
MyWidget
会重新构建。
自定义高亮效果
你可以通过传递 color
和 duration
参数来自定义高亮效果的颜色和持续时间。
HighlightOnUpdate(
color: Colors.red,
duration: Duration(milliseconds: 500),
child: MyWidget(),
)