你知道吗,Flutter内置了10多种show

移动开发 作者: 2024-08-25 01:40:02
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 showDialog showDialog 用于弹出Mat
showDialog(
    context: context,builder: (context) {
      return AlertDialog(
        ...
      );
    }
);
Navigator.of(context,rootNavigator: true).pop(result)
Navigator.pop(context,result)
showCupertinoDialog(
    context: context,builder: (context) {
      return CupertinoAlertDialog(
       ...
      );
    });
showGeneralDialog(
    context: context,barrierDismissible:true,barrierLabel: '',transitionDuration: Duration(milliseconds: 200),pageBuilder: (BuildContext context,Animation<double> animation,Animation<double> secondaryAnimation) {
      return Center(
        child: Container(
          height: 300,width: 250,color: Colors.lightGreenAccent,),);
    });
showGeneralDialog(
    context: context,barrierColor: Colors.black.withOpacity(.5),...
  )
showGeneralDialog(
    transitionBuilder: (BuildContext context,Animation<double> secondaryAnimation,Widget child) {
      return ScaleTransition(scale: animation,child: child);
    },...
  )
showAboutDialog(
  context: context,applicationIcon: Image.asset(
    'images/bird.png',height: 100,width: 100,applicationName: '应用程序',applicationVersion: '1.0.0',applicationLegalese: 'copyright 老孟,一枚有态度的程序员',children: <Widget>[
    Container(
      height: 30,color: Colors.red,Container(
      height: 30,color: Colors.blue,color: Colors.green,)
  ],);
  • applicationIcon:应用程序的图标。
  • applicationName:应用程序名称。
  • applicationVersion:应用程序版本。
  • applicationLegalese:著作权(copyright)的提示。
  • children:位置如上图的红蓝绿色的位置。
  1. pubspec.yaml中配置支持国际化:
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  1. 在MaterialApp中配置当前区域:
MaterialApp(
      title: 'Flutter Demo',localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,],supportedLocales: [
        const Locale('zh','CH'),const Locale('en','US'),locale: Locale('zh'),...
  )
showLicensePage(
  context: context,);
showBottomSheet(
    context: context,builder: (context) {
      return Container(height: 200,color: Colors.lightBlue);
    });
showBottomSheet(
    context: context,backgroundColor: Colors.lightGreenAccent,elevation:20,shape: CircleBorder(),builder: (context) {
      return Container(height: 200);
    });
showModalBottomSheet(
        context: context,builder: (BuildContext context) {
          return BottomSheet(...);
        });
showModalBottomSheet(
    context: context,backgroundColor: Colors.lightBlue,elevation: 10,shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),...
  )
showModalBottomSheet(
    context: context,isScrollControlled: true,builder: (BuildContext context) {
      return ListView.builder(
        itemBuilder: (context,index) {
          return ListTile(
            title: Text('老孟$index'),);
        },itemExtent: 50,itemCount: 50,);
    });
showCupertinoModalPopup(
    context: context,builder: (BuildContext context) {
      return CupertinoActionSheet(
        title: Text('提示'),message: Text('是否要删除当前项?'),actions: <Widget>[
          CupertinoActionSheetAction(
            child: Text('删除'),onPressed: () {},isDefaultAction: true,CupertinoActionSheetAction(
            child: Text('暂时不删'),isDestructiveAction: true,);
    }
);
showCupertinoModalPopup(
    context: context,filter: ImageFilter.blur(sigmaX: 5.0,sigmaY: 5.0),...
  )
showMenu(
    context: context,position: RelativeRect.fill,items: <PopupMenuEntry>[
      PopupMenuItem(child: Text('语文')),PopupMenuDivider(),CheckedPopupMenuItem(
        child: Text('数学'),checked: true,PopupMenuItem(child: Text('英语')),]);
final RenderBox button = context.findRenderObject();
final RenderBox overlay = Overlay.of(context).context.findRenderObject();
final RelativeRect position = RelativeRect.fromRect(
  Rect.fromPoints(
    button.localToGlobal(Offset(0,0),ancestor: overlay),button.localToGlobal(button.size.bottomRight(Offset.zero),Offset.zero & overlay.size,);
showSearch(context: context,delegate: CustomSearchDelegate());

class CustomSearchDelegate extends SearchDelegate<String>{
  @override
  List<Widget> buildActions(BuildContext context) {
    return null;
  }

  @override
  Widget buildLeading(BuildContext context) {
    return null;
  }

  @override
  Widget buildResults(BuildContext context) {
    return null;
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return null;
  }

}
@override
Widget buildLeading(BuildContext context) {
  return IconButton(
    icon: Icon(Icons.arrow_back,onPressed: (){
      close(context,'');
    },);
}
@override
Widget buildSuggestions(BuildContext context) {
  return ListView.separated(
    itemBuilder: (context,index) {
      return ListTile(
        title: Text('老孟 $index'),onTap: () {
          query = '老孟 $index';
        },);
    },separatorBuilder: (context,index) {
      return Divider();
    },itemCount: Random().nextInt(5),);
}
@override
List<Widget> buildActions(BuildContext context) {
  return [
    IconButton(
      icon: Icon(
        Icons.clear,onPressed: () {
        query = '';
      },)
  ];
}
@override
Widget buildResults(BuildContext context) {
  return ListView.separated(
    itemBuilder: (context,index) {
      return Container(
        height: 60,alignment: Alignment.center,child: Text(
          '$index',style: TextStyle(fontSize: 20),itemCount: 10,);
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68133.html