Skip to content

Commit

Permalink
feat(screens): Support refresh login captcha image
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Nov 27, 2023
1 parent 6e80f6d commit 7700f09
Showing 1 changed file with 62 additions and 16 deletions.
78 changes: 62 additions & 16 deletions lib/screens/login/captcha_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import 'package:tsdm_client/generated/i18n/strings.g.dart';
import 'package:tsdm_client/providers/net_client_provider.dart';
import 'package:tsdm_client/utils/debug.dart';

/// Captcha image size is 320x150.
const _captchaImageWidth = 320;
const _captchaImageHeight = 150;

/// Row height is 60 (Default).
/// So indicator should use (150 / 60) * 320 width.
const _indicatorBoxWidth = (60 / _captchaImageHeight) * _captchaImageWidth;

class CaptchaImage extends ConsumerStatefulWidget {
const CaptchaImage({super.key});

Expand All @@ -18,27 +26,65 @@ class CaptchaImage extends ConsumerStatefulWidget {
}

class _VerityImageState extends ConsumerState<CaptchaImage> {
/// Debounce refreshing.
bool refreshDebounce = false;

/// Need this variable to mark whether the future [f] is completed or not.
/// Because when refreshing state triggered by user interaction, it's weired
/// that the [FutureBuilder] below has the previous data and does not show
/// [CircularProgressIndicator] as planned.
bool futureComplete = false;
late Future<Response<dynamic>> f;

@override
Widget build(BuildContext context) {
debug('fetching login captcha');
return FutureBuilder(
future: ref.read(netClientProvider()).getUri(
CaptchaImage._fakeFormVerifyUri,
options: Options(responseType: ResponseType.bytes),
),
builder: (context, snapshot) {
if (snapshot.hasError) {
final message = t.loginPage.failedToGetCaptcha(err: snapshot.error!);
debug(message);
return Text(message);
f = ref
.read(netClientProvider())
.getUri(
CaptchaImage._fakeFormVerifyUri,
// Uri.parse('https://source.unsplash.com/random/320x150'),
options: Options(responseType: ResponseType.bytes),
)
.whenComplete(() {
futureComplete = true;
});
return GestureDetector(
onTap: () async {
if (refreshDebounce) {
return;
}

if (snapshot.hasData) {
final bytes = Uint8List.fromList(snapshot.data!.data as List<int>);
return Image.memory(bytes);
}
return const CircularProgressIndicator();
setState(() {
refreshDebounce = true;
futureComplete = false;
});
debug('refresh login captcha');
await Future.delayed(const Duration(milliseconds: 4000), () {
refreshDebounce = false;
});
},
child: FutureBuilder(
future: f,
builder: (context, snapshot) {
if (snapshot.hasError) {
final message =
t.loginPage.failedToGetCaptcha(err: snapshot.error!);
debug(message);
return Text(message);
}

if (snapshot.hasData && futureComplete) {
final bytes = Uint8List.fromList(snapshot.data!.data as List<int>);
debug('fetch login captcha finished, ${f.hashCode}');
return Image.memory(bytes, height: 60);
}
return const SizedBox(
width: _indicatorBoxWidth,
child: Center(
child: CircularProgressIndicator(),
));
},
),
);
}
}

0 comments on commit 7700f09

Please sign in to comment.