如何在 buttonPressed 的 showModel 中显示不同的 Widget

How to Show different Widget in showModel on buttonPressed

我正在尝试显示 showModelBottomSheet(),它运行良好,bottomSheet 中有一个按钮,所以每当我按下此按钮时,它都会显示/return 一个新的容器(容器不同的小部件)。在这个 onTap() 上,我已经将 bool 变量设置为 true 或 false ...但是 work.Does setState(());方法在 ModelBottomSheet 中不起作用?和 showGeneralDialog() 的类似案例... 我该如何解决这两个问题。

bool p=true;
showModalBottomSheet(
  backgroundColor: Colors.transparent,
    enableDrag: true,
    isScrollControlled: true,
    context: context, builder: (BuildContext context){

  return p==true?Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height/2,
    padding: EdgeInsets.all(5.0),
    decoration: BoxDecoration(
      color: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0),topRight: Radius.circular(20.0)),
    ),
    child: Column(
      children: <Widget>[
        Container(
          alignment: Alignment.topLeft,
          width: MediaQuery.of(context).size.width,
          height: 30.0,
          child: IconButton(icon: Icon(Icons.arrow_downward,color: Colors.white,size: 32.0,), onPressed: (){
            Navigator.pop(context);
          }),
        ),

        Text(widget.model.cName,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 25.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('AMOUNT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.w100,fontSize: 12.0),),

        SizedBox(height: 20.0,),
        Text(widget.model.b,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 32.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('REPEAT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.bold,fontSize: 12.0,fontFamily: 'Rajdhani'),),
        SizedBox(height: 15.0,),
        Text('EVERY MONTH AT   '+widget.model.date,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 20.0,fontFamily: 'Rajdhani'),),


        SizedBox(height: 20.0,),
        GestureDetector(
          onTap: (){
            pay=false;
            print('ayeeee $p');
          },
          child: Container(
            alignment: Alignment.bottomCenter,
            width: MediaQuery.of(context).size.width*0.8,
            height: 40.0,
            decoration: BoxDecoration(color: Colors.white,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Center(
              child: Text('P ',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 18.0,fontFamily: 'Rajdhani')),
            ),
          ),
        ),




      ],
    ),
  ):Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height/2,
    padding: EdgeInsets.all(5.0),
    decoration: BoxDecoration(
      color: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0),topRight: Radius.circular(20.0)),
    ),
    child: Column(
      children: <Widget>[
        Container(
          alignment: Alignment.topLeft,
          width: MediaQuery.of(context).size.width,
          height: 30.0,
          child: IconButton(icon: Icon(Icons.arrow_downward,color: Colors.white,size: 32.0,), onPressed: (){
            Navigator.pop(context);
          }),
        ),

        Text('AMOUNT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.w100,fontSize: 12.0),),

        SizedBox(height: 20.0,),
        Text(widget.model.b,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 32.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('SOURCE',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.bold,fontSize: 12.0,fontFamily: 'Rajdhani'),),
        SizedBox(height: 15.0,),
        Text('EVERY MONTH AT   '+widget.model.date,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 20.0,fontFamily: 'Rajdhani'),),


        SizedBox(height: 20.0,),
        Container(
          alignment: Alignment.bottomCenter,
          width: MediaQuery.of(context).size.width*0.8,
          height: 40.0,
          decoration: BoxDecoration(color: Colors.white,
            borderRadius: BorderRadius.circular(10.0),
          ),
          child: Center(
            child: Text('PA ',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 18.0,fontFamily: 'Rajdhani')),
          ),
        ),




      ],
    ),
  );

要更新底部 sheet 的 state,您需要用 StatefulBuilder 包裹您的小部件,并使用提供的 StateSetter 来设置状态:

我在下面添加了一个例子:

showModalBottomSheet(
        context: context,
        builder: (context) {
          return StatefulBuilder(
              builder: (BuildContext context, StateSetter setModalState) {
            return  // ** YOUR WIDGETS ** 
          });
    });