代码拉取完成,页面将自动刷新
import 'dart:math';
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
import 'package:example/indicators/warp_indicator.dart';
import 'package:example/widgets/example_app_bar.dart';
import 'package:example/widgets/example_list.dart';
import 'package:flutter/material.dart';
class ProgrammaticallyControlled extends StatefulWidget {
const ProgrammaticallyControlled({Key? key}) : super(key: key);
@override
State<ProgrammaticallyControlled> createState() =>
_ProgrammaticallyControlledState();
}
class _ProgrammaticallyControlledState
extends State<ProgrammaticallyControlled> {
final key = GlobalKey<CustomRefreshIndicatorState>();
final controller = IndicatorController(refreshEnabled: false);
bool _startedManually = false;
int _itemsCount = 4;
Future<void> onRefresh() async {
await Future.delayed(const Duration(seconds: 2));
if (!mounted) return;
setState(() {
_itemsCount = Random().nextInt(4) + 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: ExampleAppBar(
title: "Programmatically-controlled",
actions: [
AnimatedBuilder(
animation: controller,
builder: (context, _) {
return Row(
children: [
IconButton(
onPressed: controller.isIdle
? () {
key.currentState!.show(
draggingCurve: Curves.easeOutBack,
);
_startedManually = true;
}
: null,
icon: const Icon(Icons.play_arrow),
),
IconButton(
onPressed: controller.isIdle
? () => key.currentState!.refresh(
draggingCurve: Curves.easeOutBack,
)
: null,
icon: const Icon(Icons.refresh),
),
IconButton(
onPressed: controller.isLoading && _startedManually
? () {
key.currentState!.hide();
_startedManually = false;
}
: null,
icon: const Icon(Icons.stop),
),
],
);
},
),
],
),
body: WarpIndicator(
controller: controller,
indicatorKey: key,
onRefresh: onRefresh,
child: ExampleList(itemCount: _itemsCount),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。