#flutter
#flutter
Вопрос:
Я новичок в flutter, у меня есть FlutterToast
в моем приложении flutter, которое вызывается при нажатии кнопки. Мой вопрос
Если возможно добавить начальный значок внутри в FlutterToast
Flutter Toast:
Fluttertoast.showToast(
msg: "Press and hold to send Alert!",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.white,
textColor: Colors.black87,
fontSize: 16.0
);
Ответ №1:
Вы можете использовать FToast из того же пакета, который используете. Вместо некоторого текста требуется дочерний виджет, который, я думаю, является более гибким.
Вот быстрая реализация, которая делает то, что вы хотите:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FToast fToast;
@override
void initState() {
super.initState();
fToast = FToast();
fToast.init(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
fToast.showToast(
toastDuration: Duration(milliseconds: 500),
child: Material(
color: Colors.white,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.face),
Text(
"Press and hold to send Alert!",
style: TextStyle(color: Colors.black87, fontSize: 16.0),
)
],
),
),
gravity: ToastGravity.CENTER,
);
},
),
body: Center(
child: Text('Text'),
),
);
}
}
Комментарии:
1. Как мне увеличить
toastDuration
?
Ответ №2:
Проверьте этот пакет https://pub.dev/packages/flutter_flexible_toast Вы можете установить начальный значок в его конструкторе
Ответ №3:
Конечно, вы можете настроить дочерний виджет по своему усмотрению.
Редактировать:
Как подчеркнул @Lulupointu, вам нужно использовать FToast из того же пакета.
FToast fToast;
fToast.showToast(
// In here!
child: customizedLeadingIconWidget,
gravity: ToastGravity.CENTER,
);
И ваш настроенный виджет будет выглядеть так:
Widget customizedLeadingIconWidget = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: Colors.greenAccent,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.check, // Set your own leading icon!
color: Colors.red,
),
SizedBox(
width: 12.0,
),
Text("This is a Custom Toast"),
],
),
);
Комментарии:
1. У Fluttertoast нет дочернего параметра
2. Мы говорим об одном и том же пакете? github.com/ponnamkarthik/FlutterToast/blob/master/lib /…
3. Да, при этом я не могу добавить дочернего элемента в Fluttertoast.showToast Какая у вас версия? У меня есть 7.1.1
4. Хорошо, вы правы. Я не понял разницы между ними. Извините, я виноват.
5. Нет проблем! Спасибо, поймите это так 🙂
Ответ №4:
Показывать тосты с помощью значка или настраивать: используйте этот гибкий тост Flutter
dependencies:
flutter_flexible_toast: ^0.1.4
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_flexible_toast/flutter_flexible_toast.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
initState() {
super.initState();
}
void showLongToast() {
FlutterFlexibleToast.showToast(
message: "Long Simple Toast",
toastLength: Toast.LENGTH_LONG,
);
}
void showColoredToast() {
FlutterFlexibleToast.showToast(
message: "Short red Success Toast",
toastLength: Toast.LENGTH_SHORT,
backgroundColor: Colors.red,
icon: ICON.SUCCESS,
fontSize: 16,
imageSize: 35,
textColor: Colors.white);
}
void showShortToast() {
FlutterFlexibleToast.showToast(
message: "Short Info 1 Sec Toast",
toastLength: Toast.LENGTH_SHORT,
icon: ICON.INFO,
timeInSeconds: 1);
}
void showTopShortToast() {
FlutterFlexibleToast.showToast(
message: "Top Short Warning 1 Sec Toast",
toastLength: Toast.LENGTH_SHORT,
toastGravity: ToastGravity.TOP,
icon: ICON.WARNING,
timeInSeconds: 1);
}
void showCenterShortToast() {
FlutterFlexibleToast.showToast(
message: "Center Short Warning 1 Sec Toast",
toastLength: Toast.LENGTH_SHORT,
toastGravity: ToastGravity.CENTER,
icon: ICON.WARNING,
timeInSeconds: 1);
}
void showCenterShortLoadingToast() {
FlutterFlexibleToast.showToast(
message: "Short Loading 2 Sec Toast",
toastLength: Toast.LENGTH_LONG,
toastGravity: ToastGravity.BOTTOM,
icon: ICON.LOADING,
radius: 20,
elevation: 10,
textColor: Colors.white,
backgroundColor: Colors.black,
timeInSeconds: 2);
}
void cancelToast() {
FlutterFlexibleToast.cancel();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: new Scaffold(
appBar: new AppBar(
title: new Text('Flutter Flexible Toast'),
),
body: new Center(
child: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Long Simple Toast'),
onPressed: showLongToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Short Info 1 Sec Toast'),
onPressed: showShortToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Center Short Warning 1 Sec Toast'),
onPressed: showCenterShortToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Top Short Warning 1 Sec Toast'),
onPressed: showTopShortToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Short red Success Toast'),
onPressed: showColoredToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Short Loading 2 Sec Toast'),
onPressed: showCenterShortLoadingToast),
),
new Padding(
padding: const EdgeInsets.all(10.0),
child: new RaisedButton(
child: new Text('Cancel Toasts'),
onPressed: cancelToast,
),
),
],
),
),
),
);
}
}