Flutter:页面中触发点击事件,通过id更新特定视图
当页面触发某些事件后,我不想整个视图都更新,而是通过id去更新特定的一块内容
controller
class StartController extends GetxController {
StartController();
String title = "";
void onTap(int ticket) {
title = "GetBuilder -> 点击了第 $ticket 个按钮";
update(['splash_title']);
}
}
view
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'index.dart';
class StartPage extends GetView<StartController> {
const StartPage({super.key});
// 主视图
Widget _buildView() {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// 文字标题
GetBuilder<StartController>(
id: "splash_title",
builder: (_) {
return Center(
child: Text(controller.title),
);
},
),
// 按钮
ElevatedButton(
onPressed: () {
controller.onTap(DateTime.now().microsecondsSinceEpoch);
},
child: const Text("立刻点击"),
),
],
);
}
@override
Widget build(BuildContext context) {
return GetBuilder<StartController>(
init: StartController(),
id: "start",
builder: (_) {
return Scaffold(
appBar: AppBar(title: const Text("start")),
body: SafeArea(
child: _buildView(),
),
);
},
);
}
}