flutter Toast 도구 클래스
7142 단어 flutter
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:back_button_interceptor/back_button_interceptor.dart';
import 'package:funya/util/size_util.dart';
const Color _bgColor = Color.fromRGBO(51, 51, 51, 0.8);
const Color _contentColor = Color.fromRGBO(255, 255, 255, 1);
const double _textFontSize = 14.0;
const double _radius = 12.0;
const double _imgWH = 30.0;
const int _time = 1;
enum _Orientation { horizontal, vertical }
class ToastUtil {
static Future showText(
BuildContext context, {
@required String msg,
int closeTime = _time,
}) {
return _showToast(
context: context, msg: msg, stopEvent: true, closeTime: closeTime);
}
static Future showSuccess(
BuildContext context, {
@required String msg,
int closeTime = _time,
}) {
Widget img = Image.asset("assets/toast_sccuess.png", width: _imgWH);
return _showToast(
context: context,
msg: msg,
image: img,
stopEvent: true,
closeTime: closeTime);
}
static _HideCallback showLoadingText_iOS(
BuildContext context, {
String msg = " ...",
}) {
Widget img = Image.asset("assets/loading.gif", width: _imgWH);
return _showJhToast(
context: context,
msg: msg,
image: img,
isLoading: false,
stopEvent: true);
}
}
Future _showToast(
{@required BuildContext context,
String msg,
stopEvent = false,
Widget image,
int closeTime,
_Orientation orientation = _Orientation.vertical}) {
msg = msg;
var hide = _showJhToast(
context: context,
msg: msg,
isLoading: false,
stopEvent: stopEvent,
image: image,
orientation: orientation);
return Future.delayed(Duration(seconds: closeTime), () {
hide();
});
}
typedef _HideCallback = Future Function();
class JhToastWidget extends StatelessWidget {
const JhToastWidget({
Key key,
@required this.msg,
this.image,
@required this.isLoading,
@required this.stopEvent,
@required this.orientation,
}) : super(key: key);
final bool stopEvent;
final Widget image;
final String msg;
final bool isLoading;
final _Orientation orientation;
@override
Widget build(BuildContext context) {
SizeUtil.init(context, width: 375, height: 812, allowFontScaling: false);
Widget topW;
bool isHidden;
if (this.isLoading == true) {
isHidden = false;
topW = CircularProgressIndicator(
strokeWidth: 3.0,
valueColor: AlwaysStoppedAnimation(_contentColor),
);
} else {
isHidden = image == null ? true : false;
topW = image;
}
var widget = Material(
// color: Colors.yellow,
color: Colors.transparent,
child: Align(
// alignment: Alignment.center,
alignment: Alignment(0.0, -0.2), //
child: Container(
margin: const EdgeInsets.all(50.0),
padding:
EdgeInsets.symmetric(horizontal: 24.0.w, vertical: 16.0.w),
decoration: BoxDecoration(
color: _bgColor,
borderRadius: BorderRadius.circular(_radius),
),
child: ClipRect(
child: orientation == _Orientation.vertical
? Column(
mainAxisSize: MainAxisSize.min,
children: [
Offstage(
offstage: isHidden,
child: Container(
width: 40.0.w,
height: 40.0.w,
margin: EdgeInsets.only(bottom: 8.0.w),
padding: EdgeInsets.all(4.0),
child: topW,
),
),
Text(msg,
style: TextStyle(
fontSize: _textFontSize,
color: _contentColor),
textAlign: TextAlign.center),
],
)
: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Offstage(
offstage: isHidden,
child: Container(
width: 36.0.w,
height: 36.0.w,
margin: EdgeInsets.only(right: 8.0.w),
padding: EdgeInsets.all(4.0),
child: topW,
),
),
Text(msg,
style: TextStyle(
fontSize: _textFontSize,
color: _contentColor),
textAlign: TextAlign.center),
],
),
),
)));
return IgnorePointer(
ignoring: !stopEvent,
child: widget,
);
}
}
int backButtonIndex = 2;
_HideCallback _showJhToast({
@required BuildContext context,
@required String msg,
Widget image,
@required bool isLoading,
bool stopEvent = false,
_Orientation orientation = _Orientation.vertical,
}) {
Completer result = Completer();
var backButtonName = 'funya$backButtonIndex';
BackButtonInterceptor.add((stopDefaultButtonEvent) {
result.future.then((hide) {
hide();
});
return true;
}, zIndex: backButtonIndex, name: backButtonName);
backButtonIndex++;
var overlay = OverlayEntry(
maintainState: true,
builder: (_) => WillPopScope(
onWillPop: () async {
var hide = await result.future;
hide();
return false;
},
child: JhToastWidget(
image: image,
msg: msg,
stopEvent: stopEvent,
isLoading: isLoading,
orientation: orientation,
),
));
result.complete(() {
if (overlay == null) {
return;
}
overlay.remove();
overlay = null;
BackButtonInterceptor.removeByName(backButtonName);
});
Overlay.of(context).insert(overlay);
return () async {
var hide = await result.future;
hide();
};
}
사용 방법:
var hide = ToastUtil.showLoadingText_iOS(context, msg: " ...");
Future.delayed(Duration(seconds: 2), () {
ToastUtil.showSuccess(context, msg: ' ');
hide();
});
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콘텐츠 SaaS | 모바일 네이티브 개발용 Flutter SDK기본 모바일 지원을 위해 Bloomreach Content Flutter SDK로 시작하세요. Flutter는 단일 코드베이스에서 아름답고 고유하게 컴파일된 다중 플랫폼 애플리케이션을 빌드하기 위한 오픈 소스 프레임...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.