[英]how to add square container in a border of a container in flutter
嘗試這個,
Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Stack(
children: [
Container(
height: 200,
decoration: BoxDecoration(
border: Border.all(width: 4, color: Colors.black),
),
),
Container(
width: double.infinity,
height: 100,
margin: EdgeInsets.only(top: 150, left: 80, right: 80),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(width: 4, color: Colors.black)),
),
],
),
)
請參考下面的代碼,你可以使用堆棧和定位
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
// Application name
title: 'Flutter Hello World',
// Application theme data, you can set the colors for the application as
// you want
theme: ThemeData(
primarySwatch: Colors.blue,
),
// A widget which will be started on application startup
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// The title text which will be shown on the action bar
title: Text("title"),
),
body: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
height: 130.0,
width: 190.0,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 2.0,
),
),
),
Positioned(
bottom: -40.0,
child: Container(
height: 90.0,
width: 130.0,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.black,
width: 2.0,
),
),
),
),
],
),
);
}
}
嘗試將 Stack 與 Alignment 結合使用
MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
// The title text which will be shown on the action bar
title: Text("title"),
),
body: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
width: size.width * 0.40,
height: size.height * 0.20,
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 2.0,
),
),
),
Align(
alignment: Alignment(0, 0.25),
child: Container(
width: size.width * 0.25,
height: size.height * 0.15,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.black,
width: 2.0,
),
),
),
),
],
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.