透明背景颤振角半径

下面是我的代码,我希望渲染一个圆角容器与透明的背景。

return new Container(
//padding: const EdgeInsets.all(32.0),
height: 800.0,
//color: const Color(0xffDC1C17),
//color: const Color(0xffFFAB91),
decoration: new BoxDecoration(
color: Colors.green, //new Color.fromRGBO(255, 0, 0, 0.0),
borderRadius: new BorderRadius.only(
topLeft:  const  Radius.circular(40.0),
topRight: const  Radius.circular(40.0))
),
child:  new Container(
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.only(
topLeft:  const  Radius.circular(40.0),
topRight: const  Radius.circular(40.0))
),
child: new Center(
child: new Text("Hi modal sheet"),
)


),

然而,这是它呈现的,它呈现一个圆角半径的白色容器(预期透明)。有什么帮助吗?

screenshot

251480 次浏览

如果你包装你的 Container与圆角内的父母与背景颜色设置为 Colors.transparent我认为这样做你正在寻找。如果使用 Scaffold,默认的背景颜色是白色。如果达到了你想要的,改为 Colors.transparent

        new Container(
height: 300.0,
color: Colors.transparent,
child: new Container(
decoration: new BoxDecoration(
color: Colors.green,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(40.0),
topRight: const Radius.circular(40.0),
)
),
child: new Center(
child: new Text("Hi modal sheet"),
)
),
),
/// Create the bottom sheet UI
Widget bottomSheetBuilder(){
return Container(
color: Color(0xFF737373), // This line set the transparent background
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16.0),
topRight: Radius.circular( 16.0)
)
),
child: Center( child: Text("Hi everyone!"),)
),
);
}

调用这个命令可以显示带有角的 BotoomSheet:

/// Show the bottomSheet when called
Future _onMenuPressed() async {
showModalBottomSheet(
context: context,
builder: (widgetBuilder) => bottomSheetBuilder()
);
}

这是个古老的问题,但是对于那些遇到这个问题的人来说..。

圆角后面的白色背景实际上不是容器,而是应用程序的画布颜色。

修复方法: 将应用程序的画布颜色更改为 Colors.arency

例如:

return new MaterialApp(
title: 'My App',
theme: new ThemeData(
primarySwatch: Colors.green,
canvasColor: Colors.transparent, //----Change to this------------
accentColor: Colors.blue,
),
home: new HomeScreen(),
);

如果希望使用透明背景圆角,最佳方法是使用 ClipRect。

return ClipRRect(
borderRadius: BorderRadius.circular(40.0),
child: Container(
height: 800.0,
width: double.infinity,
color: Colors.blue,
child: Center(
child: new Text("Hi modal sheet"),
),
),
);

从2019年5月1日起,使用 BottomSheetTheme

MaterialApp(
theme: ThemeData(
// Draw all modals with a white background and top rounded corners
bottomSheetTheme: BottomSheetThemeData(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(10))
)
)
),

最近引入的 ,使用主题来控制工作表样式应该是最好的方法。

If you want to theme different bottom sheets differently, include a new Theme object in the appropriate subtree to override the bottom sheet theme just for that area.

如果出于某种原因,您仍然希望在启动底部工作表时手动覆盖主题,那么 showBottomSheetShowModalBottomsheet现在有一个 背景颜色参数。像这样使用它:

 showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (c) {return NavSheet();},
);

模板底部使用透明的背景颜色,盒子装饰使用单独的颜色


   showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context, builder: (context) {
return Container(


decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft:Radius.circular(40) ,
topRight: Radius.circular(40)
),
),
padding: EdgeInsets.symmetric(vertical: 20,horizontal: 60),
child: Settings_Form(),
);
});
Scaffold(
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Container(
height: 200,
width: 170,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: Color(
0xFF1D1E33,
),
borderRadius: BorderRadius.circular(5),
),
),
);
class MyApp2 extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
appBarTheme: AppBarTheme(
elevation: 0,
color: Colors.blueAccent,
)
),
title: 'Welcome to flutter ',
home: HomePage()
);
}
}


class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage> {


int number = 0;
void _increment(){
setState(() {
number ++;
});
}


@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueAccent,
appBar: AppBar(
title: Text('MyApp2'),
leading: Icon(Icons.menu),
// backgroundColor: Colors.blueAccent,


),
body: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(200.0),
topRight: Radius.circular(200)
),
color: Colors.white,
),
)
);
}
}

用于处理这一问题的三个相关的一揽子方案包括许多先进的备选方案:

showModalBottomSheet(
context: context,
builder: (context) => Container(
color: Color(0xff757575), //background color
child:  new Container(
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.only(
topLeft:  const  Radius.circular(40.0),
topRight: const  Radius.circular(40.0))
),
child: new Center(
child: new Text("Hi modal sheet"),
)


     



),


)

这将使您的容器颜色与背景颜色相同。将有一个具有相同高度宽度和蓝色的子容器。 这将使角落的颜色与背景颜色相同。

如果两个容器是兄弟容器,并且底部容器有圆角 顶部容器是动态的,那么你就必须使用堆栈小部件

Stack(
children: [
/*your_widget_1*/,
/*your_widget_2*/,
],
);