有趣的範例-最上層是顯示三個文字按鈕,但不顯示邊框,第二層是顯示按鈕邊框,不顯示文字,第三層顯示與按鈕同高的灰色底色框
最上層是顯示三個文字按鈕,但不顯示邊框,第二層是顯示按鈕邊框,不顯示文字,第三層顯示與按鈕同高的灰色底色框
三層是重疊的
最上層有三個按鈕
第一層的三個按鈕是並排,第二層只有一個按鈕
第一層按鈕是橫向並排
import 'package:flutter/material.dart'; class LayeredButtonsPage extends StatefulWidget { @override _LayeredButtonsPageState createState() => _LayeredButtonsPageState(); } class _LayeredButtonsPageState extends State<LayeredButtonsPage> { int selectedButtonIndex = -1; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Layered Buttons Page'), ), body: Center( child: Stack( alignment: Alignment.center, children: [ // 第三层:底色框 Container( width: double.infinity, height: 48, margin: EdgeInsets.symmetric(horizontal: 16), decoration: BoxDecoration( color: Colors.grey[200], // 灰色背景色 borderRadius: BorderRadius.circular(8), ), ), // 第二层:按钮边框 AnimatedContainer( duration: Duration(milliseconds: 200), width: selectedButtonIndex >= 0 ? double.infinity : 0, height: 48, margin: EdgeInsets.symmetric(horizontal: 16), decoration: BoxDecoration( border: Border.all( color: Colors.blue, width: 2, ), borderRadius: BorderRadius.circular(8), ), ), // 第一层:文字按钮 Row( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () { setState(() { selectedButtonIndex = 0; }); }, child: Text('Button 1'), style: ElevatedButton.styleFrom( elevation: 0, primary: Colors.transparent, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), ), ElevatedButton( onPressed: () { setState(() { selectedButtonIndex = 1; }); }, child: Text('Button 2'), style: ElevatedButton.styleFrom( elevation: 0, primary: Colors.transparent, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), ), ElevatedButton( onPressed: () { setState(() { selectedButtonIndex = 2; }); }, child: Text('Button 3'), style: ElevatedButton.styleFrom( elevation: 0, primary: Colors.transparent, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), ), ], ), ], ), ), ); } } void main() { runApp(MaterialApp( home: LayeredButtonsPage(), )); }
留言
張貼留言