0

我有一个表情符号列表[, , , , , , , , , , , , , ],我也有一个按钮,没有与之关联的图标。请参阅此材质按钮。

                     MaterialButton(
                        onPressed: () {},
                        
                        child: Text("",textScaleFactor: 2),
                        shape: CircleBorder(),
                        padding: EdgeInsets.all(16),
                      ),

[, , , , , , , , , , , , , ]现在,我需要在延迟 1 秒后在此按钮内显示上述列表。

4

2 回答 2

2

你也可以使用未来。

MaterialButton(
 onPressed: () async {
     await Future<void>.delayed(Duration(seconds: 1));
     // your function
     },
 child: Text('', textScaleFactor: 2),
 shape: CircleBorder(),
 padding: EdgeInsets.all(16),
),
于 2021-04-17T08:18:53.010 回答
1

您可以使用 Timer.periodic 来实现这一点。请参阅此示例代码

链接到文档


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Timer emojiTimer;

  @override
  void initState() {
    emojiTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      changeEmoji();
    });
    super.initState();
  }

  int randomInt = 0;
  Future<void> changeEmoji() async {
    setState(() {
      var ran = Random();
      randomInt = ran.nextInt(emojiList.length);
    });
  }

  var emojiList = [
    "",
    "",
    " ",
    "",
    " ",
    " ",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text('hello there'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          emojiTimer.cancel();
        },
        child: Text(emojiList[randomInt]),
      ),
    );
  }
}

于 2021-04-17T08:09:45.140 回答