flutter 自定义侧边栏showDialog

showDialog+添加平移动画效果

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../common/Global.dart';
import '../../common/colors.dart';
import '../../router.dart';
import '../widget/myText.dart';

//小说目录
class NovelCatalogue extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => NovelCatalogueState();
}

class NovelCatalogueState extends State<NovelCatalogue> with SingleTickerProviderStateMixin{
  bool hasShowCatalogue = false;
  late AnimationController controller;
  late Animation<Offset> animation;

  @override
  void initState() {
    controller = AnimationController(
        duration: const Duration(milliseconds: 200), vsync: this);
    animation = Tween(begin: const Offset(1, 0), end: const Offset(0, 0))
        .animate(controller);
    if (controller.isCompleted || controller.isDismissed) {
      controller.forward();
      hasShowCatalogue=true;
    }
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _requestPop,
      child: SlideTransition(
        position: animation,
        child: GestureDetector(
          onTap: () {
            hasShowCatalogue=false;
            controller.reverse();
            Future.delayed(Duration(milliseconds: 100), () {
              MyRouter.pop(context);
            });
          },
          child: Material(
            color: Colors.transparent,
            child: GestureDetector(
              onTap: () {},
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    alignment: Alignment.centerRight,
                    height: Global.screenHeight,
                    width: Global.screenWidth - 70,
                    color: Colors.white,
                    child: Column(
                      children: [
                        addHeadView(),
                        Expanded(
                            child: SingleChildScrollView(
                              child: Column(
                                children: [
                                  MediaQuery.removePadding(
                                      removeTop: true,
                                      context: context,
                                      child: ListView.builder(
                                          itemCount: 10,
                                          physics:
                                          const NeverScrollableScrollPhysics(),
                                          shrinkWrap: true,
                                          itemBuilder: (BuildContext context,
                                              int index) {
                                            return Container(
                                              margin: EdgeInsets.only(
                                                  left: 10.r, right: 10.r),
                                              child: Column(
                                                children: [
                                                  Row(
                                                    children: [
                                                      ConstrainedBox(
                                                        constraints: BoxConstraints(
                                                            maxWidth: Global
                                                                .screenWidth -
                                                                20 -
                                                                70),
                                                        child: Text(
                                                          '第九百八十章 不经意的指点是一个是佛啊后方熬哦跌幅为地上',
                                                          style: TextStyle(
                                                              color: Colors
                                                                  .black,
                                                              fontSize:
                                                              16.sp),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                  Container(
                                                    margin: EdgeInsets.only(
                                                        top: 10.r,
                                                        bottom: 10.r),
                                                    height: 1,
                                                    color: Colors.grey,
                                                  )
                                                ],
                                              ),
                                            );
                                          }))
                                ],
                              ),
                            ))
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget addHeadView() {
    return Container(
        padding: EdgeInsets.only(
            top: Global.topHeight, left: 10.r, bottom: 5.r, right: 15.r),
        margin: EdgeInsets.only(bottom: 10.r),
        child:
            Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: const Image(
              image: AssetImage('assets/images/icon_back@2x.png'),
              width: 24,
              height: 25,
              color: Colors.black,
            ),
          ),
          GestureDetector(
              onTap: () {
                //点击后清空列表数据,获取最底部的数据
              },
              child: ContentText('顺序', 16.0, CommonColors.getColor1A1A1A))
        ]));
  }

  //监听物理返回键,限制返回
  Future<bool> _requestPop() {
    print('是否展示目录}');
    if(mounted){
      if (Navigator.canPop(context)) {
        if(hasShowCatalogue){
          controller.reverse();
          hasShowCatalogue=false;
          print('返回键进到这里1');
          Future.delayed(Duration(milliseconds: 100),(){
            MyRouter.pop(context);
          });
        }else{
          MyRouter.pop(context);
          print('返回键进到这里2');
        }
        // MyRouter.pop(context);
      }
    }
    return Future.value(false);
  }
}

使用时

  showBroadSide() {
    showDialog(
        useSafeArea:false,
        context: context,
        builder: (context) => NovelCatalogue());
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值