gpt4 book ai didi

flutter - 如何弹出文字?

转载 作者:行者123 更新时间:2023-12-03 03:30:44 25 4
gpt4 key购买 nike

我想在不更改屏幕任何内容的情况下,将居中文本弹出3秒钟以进行交流。一切保持不变,仅显示文本几秒钟。我怎样才能做到这一点?

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用https://pub.dev/packages/fluttertoast软件包
程式码片段

fToast.showToast(
child: Container(
color: Colors.red,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("Hello"),
Text("World"),
Text("Hello"),
Text("World"),
Text("Hello"),
Text("World"),
],
),
),
gravity: ToastGravity.CENTER,
toastDuration: Duration(seconds: 3),
);
工作演示
enter image description here
完整的代码
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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> {
int _counter = 0;
FToast fToast;

@override
void initState() {
super.initState();
fToast = FToast();
fToast.init(context);
}

void _showMessage() {
fToast.showToast(
child: Container(
color: Colors.red,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("Hello"),
Text("World"),
Text("Hello"),
Text("World"),
Text("Hello"),
Text("World"),
],
),
),
gravity: ToastGravity.CENTER,
toastDuration: Duration(seconds: 3),
);

setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _showMessage,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于flutter - 如何弹出文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64041416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com