我試圖尋找一種自定義底部導航欄的方法,如圖所示。

我仍然無法獲得確切的外觀。當它被選中但沒有標簽時,我能夠突出顯示帶有藍色框的圖示。有沒有辦法實作這一目標或采取不同的方法?很高興有人可以幫助提供代碼片段。
uj5u.com熱心網友回復:
這是我沒有任何第三方包的簡單解決方案,您可以自定義這個小部件,我相信有很多改進的余地。希望這能讓您對如何進一步發展有所了解。

import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget>
with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _bodyView = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
late TabController _tabController;
@override
void initState() {
// TODO: implement initState
super.initState();
_tabController = TabController(vsync: this, length: 3);
}
Widget _tabItem(Widget child, String label, {bool isSelected = false}) {
return AnimatedContainer(
margin: EdgeInsets.all(8),
alignment: Alignment.center,
duration: const Duration(milliseconds: 500),
decoration: !isSelected
? null
: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.black,
),
padding: const EdgeInsets.all(10),
child: Column(
children: [
child,
Text(label, style: TextStyle(fontSize: 8)),
],
));
}
final List<String> _labels = ['Home', 'maps', 'camera'];
@override
Widget build(BuildContext context) {
List<Widget> _icons = const [
Icon(Icons.home_outlined),
Icon(Icons.explore_outlined),
Icon(Icons.camera_alt_outlined)
];
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _bodyView.elementAt(_selectedIndex),
),
bottomNavigationBar: Container(
height: 100,
padding: const EdgeInsets.all(12),
child: ClipRRect(
borderRadius: BorderRadius.circular(50.0),
child: Container(
color: Colors.teal.withOpacity(0.1),
child: TabBar(
onTap: (x) {
setState(() {
_selectedIndex = x;
});
},
labelColor: Colors.white,
unselectedLabelColor: Colors.blueGrey,
indicator: const UnderlineTabIndicator(
borderSide: BorderSide.none,
),
tabs: [
for (int i = 0; i < _icons.length; i )
_tabItem(
_icons[i],
_labels[i],
isSelected: i == _selectedIndex,
),
],
controller: _tabController),
),
),
),
);
}
}
uj5u.com熱心網友回復:
試試下面的代碼希望它對你有幫助。請參閱floating_bottom_navigation_bar包裝

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/398593.html
上一篇:多提供者不改變狀態
