我有一个凸起的按钮,启动我的指纹身份验证,当Future返回时,我希望能够将Raised Button更改为新文本,并使用新的onPressed方法来完成所需的身份验证。我已经给了凸起按钮一个键,但无法找到如何操作该按钮来改变它。可能吗?有人有例子吗?
我尝试使用相同的密钥创建新的凸起按钮,具体取决于用户是否经过身份验证,但它没有更改任何内容。
任何帮助都会很棒。
答案 0 :(得分:0)
我建议您查看Flutter Interactivity Tutorial。
Future
完成后,您可以致电setState
告诉Flutter重建您的StatefulWidget
。在您的build()
方法中,您可以使用用户的经过身份验证的状态来构建不同的RaisedButton
。
以下是执行此操作的示例代码:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Local Auth Demo',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _authenticated = false;
Future<Null> _authenticate() async {
final LocalAuthentication auth = new LocalAuthentication();
bool authenticated = false;
try {
authenticated = await auth.authenticateWithBiometrics(
localizedReason: 'Scan your fingerprint to authenticate',
useErrorDialogs: true);
} on PlatformException catch (e) {
print(e);
}
if (!mounted) return;
setState(() {
_authenticated = authenticated;
});
}
Widget _buildAuthButton() {
assert(!_authenticated);
return new RaisedButton(
child: new Text('Authenticate'),
onPressed: _authenticate,
);
}
Widget _buildContinueButton() {
assert(_authenticated);
return new RaisedButton(
child: new Text('Continue'),
onPressed: () {
// Do something now that the user is authenticated
},
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Interactivity Tutoral'),
),
body: new Center(
child: _authenticated ? _buildContinueButton() : _buildAuthButton(),
),
);
}
}
答案 1 :(得分:0)
我将使用FutureBuilder,仅根据Future是否完成而返回一个小部件或另一个
new FutureBuilder<String>(
future: your_future,
builder: (_, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const CircularProgressIndicator();
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return new Text(
'Your data: ${snapshot.data}',
);
}
})