flutter学习之基础组件(八)

flutter学习之基础组件(八)

本节主要记录三方库轮播图的使用和Dialog相关组件,以及自定义dialog

文章目录

  • flutter学习之基础组件(八)
  • 一、轮播图
    • 1.引导页轮播
    • 2.Banner轮播
    • 3.卡片轮播
    • 4.其他轮播
    • 5.使用数据
  • 二、弹窗Dialog组件
    • 1.AlertDialog
    • 2.SimpleDialog
    • 2.showModalBottomSheet
    • 4.Toast
    • 5.自定义dialog
  • 总结


一、轮播图

如果您使用的是Flutter2.2.0以及Flutter2.2.0之后的版本请使用flutter_swiper_null_safety,
如果您使用的是Flutter2.2.0之前的版本请使用flutter_swiper。
具体使用文档请参考flutter_swiper
flutter_swiper_null_safety和flutter_swiper用法完全一样
flutter_swiper:
地址:https://pub.flutter-io.cn/packages/flutter_swiper
flutter_swiper_null_safety:
地址:https://pub.flutter-io.cn/packages/flutter_swiper_null_safety

1.引导页轮播

flutter学习之基础组件(八)_第1张图片

class SplashContent extends StatefulWidget {
  const SplashContent({Key? key}) : super(key: key);

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

class _SplashContentState extends State<SplashContent> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return new Image.network(
            listData[index]["imageUrl"],
            fit: BoxFit.fill,
          );
        },
        itemCount: listData.length,
        pagination: new SwiperPagination(),
        control: new SwiperControl(),
      ),
    );
  }
}

2.Banner轮播

flutter学习之基础组件(八)_第2张图片


class BannerContent extends StatefulWidget {
  const BannerContent({Key? key}) : super(key: key);

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

class _BannerContentState extends State<BannerContent> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Banner轮播"),
      ),
      body: Column(
        children: [
          //在Column中不能直接添加Swiper 需要在外层包一个Container组件
          Container(
            width: double.infinity,
            child: AspectRatio(
              aspectRatio: 16 / 9,
              child: Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return new Image.network(
                    listData[index]["imageUrl"],
                    fit: BoxFit.fill,
                  );
                },
                itemCount: listData.length,
                pagination: new SwiperPagination(),
                loop: false,
                index: 2,
                autoplay: true,
                //自动轮播
                duration: 1000,
                //   control: new SwiperControl(),
              ),
            ),
          ),
          SizedBox(
            height: 20,
          ),
          Row(
            children: [Text("我的是文本 哈哈哈")],
          )
        ],
      ),
    );
  }
}

3.卡片轮播

flutter学习之基础组件(八)_第3张图片


class CardContent extends StatefulWidget {
  const CardContent({Key? key}) : super(key: key);

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

class _CardContentState extends State<CardContent> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return new Image.network(
            listData[index]["imageUrl"],
            fit: BoxFit.fill,
          );
        },
        itemCount: listData.length,
        itemWidth: 300.0,
        itemHeight: 400.0,
        layout: SwiperLayout.TINDER,
      ),
    );
  }
}

4.其他轮播


class OtherContent extends StatefulWidget {
  const OtherContent({Key? key}) : super(key: key);

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

class _OtherContentState extends State<OtherContent> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Swiper(
          layout: SwiperLayout.CUSTOM,
          customLayoutOption: new CustomLayoutOption(
              startIndex: -1,
              stateCount: 3
          ).addRotate([
            -45.0/180,
            0.0,
            45.0/180
          ]).addTranslate([
            new Offset(-370.0, -40.0),
            new Offset(0.0, 0.0),
            new Offset(370.0, -40.0)
          ]),
          itemWidth: 300.0,
          itemHeight: 200.0,
          itemBuilder: (context, index) {
            return new Container(
       //       color: Colors.grey,
              child: new Center(
                child: Image.network(
                  listData[index]["imageUrl"],
                  fit: BoxFit.cover,
                ),
              ),
            );
          },
          itemCount: listData.length),
    );
  }
}

5.使用数据

List listData = [
  {
    "title": 'Candy Shop',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://img1.baidu.com/it/u=3487414028,2932761123&fm=26&fmt=auto&gp=0.jpg',
    "description":
        'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',
  },
  {
    "title": 'Childhood in a picture',
    "author": 'Google',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134615_U2FJK.thumb.1000_0.jpeg',
    "description":
        'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',
  },
  {
    "title": 'Alibaba Shop',
    "author": 'Alibaba',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134556_QtJAy.thumb.1000_0.jpeg',
    "description":
        'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',
  },
  {
    "title": 'Candy Shop',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134519_QduTJ.thumb.1000_0.jpeg',
    "description":
        'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',
  },
  {
    "title": 'Tornado',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134502_PYSRn.thumb.1000_0.jpeg',
    "description":
        'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',
  },
  {
    "title": 'Undo',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134337_Pd5BA.thumb.1000_0.jpeg',
    "description":
        'Flutter is Google’s mobile UI framework for crafting high-quality native experiences on iOS and Android in record time. Flutter works with existing',
  },
  {
    "title": 'white-dragon',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://c-ssl.duitang.com/uploads/item/201511/19/20151119134402_RCdAm.thumb.1000_0.jpeg',
    "description":
        'Dart is a client-optimized language for fast apps on any platform... Dart is a client-optimized language for fast apps on any platform Optimizedfor',
  }
];

二、弹窗Dialog组件

dialog有一下几种:
普通dialog:AlertDialog
选择框dialog:SimpleDialog
底部弹窗dialog:showModalBottomSheet
Toast提示:三方库实现
自定义dialog

所有的弹窗设置上barrierDismissible:false,这个属性,点击弹窗外边弹窗不会消失

1.AlertDialog

  _alertDialog() async {
    var result = await showDialog(
        barrierDismissible:false,  //表示点击灰色背景的时候是否消失弹出框,建议弹出框的时候加上此属性
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("我是AlertDialog"),
            content: Text("确定要取消吗?"),
            actions: [
              OutlinedButton(
                  onPressed: () {
                    Navigator.of(context).pop("取消");
                  },
                  child: Text("取消")),
              ElevatedButton(
                  onPressed: () {
                    Navigator.of(context).pop("确定");
                  },
                  child: Text("确定"))
            ],
          );
        });
    showToast(result);
  }在这里插入代码片

2.SimpleDialog

  _simoleDialog() async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("我是SimpleDialog"),
            children: [
              SimpleDialogOption(
                child: Text("选择框1"),
                onPressed: () {
                  Navigator.of(context).pop("选择框1");
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text("选择框2"),
                onPressed: () {
                  Navigator.of(context).pop("选择框2");
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text("选择框3"),
                onPressed: () {
                  Navigator.of(context).pop("选择框3");
                },
              ),
              Divider(),
              SimpleDialogOption(
                child: Text("选择框4"),
                onPressed: () {
                  Navigator.of(context).pop("选择框4");
                },
              ),
            ],
          );
        });
    showToast(result);
  }

2.showModalBottomSheet

 _ModalBottomSheet() async {
    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ListTile(
                  title: Text("账号密码登录"),
                  leading: Icon(Icons.account_box_rounded),
                  onTap: () {
                    Navigator.of(context).pop("账号密码登录");
                  },
                ),
                ListTile(
                  title: Text("手机号登录"),
                  leading: Icon(Icons.phone_android),
                  onTap: () {
                    Navigator.of(context).pop("手机号登录");
                  },
                ),
                ListTile(
                  title: Text("人脸识别登录"),
                  leading: Icon(Icons.account_box_outlined),
                  onTap: () {
                    Navigator.of(context).pop("人脸识别登录");
                  },
                ),
              ],
            ),
          );
        });
    showToast(result);
  }

4.Toast

地址:https://pub.flutter-io.cn/packages/fluttertoast

  _Toast() async {
    Fluttertoast.showToast(
        msg: "This is Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

  void showToast(String data){
    Fluttertoast.showToast(
        msg: data,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }

5.自定义dialog


class MyDialog extends Dialog {
  final String text;
  var timer;
  MyDialog(this.text);

  _showTimer(context) {
    timer = Timer.periodic(Duration(milliseconds: 5000), (t) {
      showToast('自动关闭dialog');
      Navigator.pop(context);
      t.cancel();
    });
  }

  void showToast(String data){
    Fluttertoast.showToast(
        msg: data,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black,
        textColor: Colors.white,
        fontSize: 16.0
    );
  }
  @override
  Widget build(BuildContext context) {
    _showTimer(context);
    return Material(
        type: MaterialType.transparency,
        child: Center(
          child: Container(
            width: 300,
            height: 200,
            color: Colors.white,
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.all(10),
                  child: Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.center,
                        child: Text("标题"),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: InkWell(
                          child: Icon(Icons.close),
                          onTap: () {
                            Navigator.pop(context);
                          },
                        ),
                      )
                    ],
                  ),
                ),
                Divider(),
                Column(
                  children: <Widget>[
                    Container(
                      height: 40,
                      child: Text(this.text),
                    )
                  ],
                ),
              ],
            ),
          ),
        ));
  }
}


总结

你可能感兴趣的:(android,flutter)