颤动使凸起的按钮在未来 return 值后发生变化
flutter make raised button change after future return value
我有一个凸起的按钮可以启动我的指纹验证,当 Future returns 我希望能够将凸起的按钮更改为新的文本和新的 onPressed 方法以完成所需的验证。我已经给了 Raised Button 一个键,但找不到如何根据该按钮进行更改。可能吗?有人有例子吗?
我尝试根据用户是否通过身份验证使用相同的密钥创建新的 Raised Button,但它没有改变任何内容。
任何帮助都会很棒。
我建议查看 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(),
),
);
}
}
我会使用 FutureBuilder,只是 return 一个小部件或另一个取决于 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}',
);
}
})
我有一个凸起的按钮可以启动我的指纹验证,当 Future returns 我希望能够将凸起的按钮更改为新的文本和新的 onPressed 方法以完成所需的验证。我已经给了 Raised Button 一个键,但找不到如何根据该按钮进行更改。可能吗?有人有例子吗?
我尝试根据用户是否通过身份验证使用相同的密钥创建新的 Raised Button,但它没有改变任何内容。
任何帮助都会很棒。
我建议查看 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(),
),
);
}
}
我会使用 FutureBuilder,只是 return 一个小部件或另一个取决于 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}',
);
}
})