【发布时间】:2021-08-04 05:34:53
【问题描述】:
我试图让一个浮动操作按钮在每次单击时更改一个文本小部件,我有 3 个类,我希望 ONE 按钮能够在每次单击按钮时调用不同的类,并且当它完成调用第三类时它回到执行第一个类,并不断重复..
我可以使用 onpressed 执行一个类,但我不确定如何在每次点击时执行不同的类..
这是代码
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void classes1(){
setState(() {
text = 'This is Text From Class1';
});
}
void classes2(){
setState(() {
text = 'This is Text From Class 2';
});
}
void classes3(){
setState(() {
text = 'This is Text From Class 3 ';
});
}
String text = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AppBar'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'$text',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: classes1,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
【问题讨论】:
标签: android flutter dart flutter-layout