網站首頁 編程語言 正文
本文實例為大家分享了flutter實現底部不規則導航欄的具體代碼,供大家參考,具體內容如下
scafford的bottomNavigationBar參數賦值BottomAppBar可以實現,BottomAppBar比BottomNavigationBar靈活,在body參數之外準備好一個fab,使用BottomAppBar的shape屬性設置BottomAppBar為一個挖了圓形的矩形,設置fab的位置便可;
main:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/EachView.dart';
import 'package:flutter_app/NewPage.dart';
void main(){
? runApp(MyApp());
}
class MyApp extends StatelessWidget{
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? // throw UnimplementedError();
? ? return new MaterialApp(
? ? ? theme: ThemeData(
? ? ? ? primarySwatch:Colors.lightBlue//fab的顏色
? ? ? ),
? ? ? home: MyNavigationBar(),
? ? );
? }
}
class ?MyNavigationBar extends StatefulWidget{
? @override
? MyNavigationBarState createState() {
? ? // TODO: implement createState
? ? // throw UnimplementedError();
? ? return new MyNavigationBarState();
? }
}
class MyNavigationBarState extends State<MyNavigationBar>{
? List<Widget> _list;
? int _index=0;
? @override
? void initState() {
? ? _list=[];
? ? _list..add(EachView(title: "0",))..add(EachView(title: "1",));
? }
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? // throw UnimplementedError();
? ? return new Scaffold(
? ? ? floatingActionButton: FloatingActionButton(//注意:如果想要fab放在默認的位置,是放在scafford的floatingactionbar參數的,而不是放在body
? ? ? ? onPressed: (){
? ? ? ? ? Navigator.push(context, MaterialPageRoute(builder: (BuildContext context){
? ? ? ? ? ? return new NewPage();
? ? ? ? ? }));
? ? ? ? },
? ? ? ? child: Icon(Icons.add,color: Colors.white,),
? ? ? ),
? ? ? floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,//設置fab的位置
? ? ? body: _list[_index],
? ? ? bottomNavigationBar: BottomAppBar(
? ? ? ? color: Colors.amber,
? ? ? ? shape: CircularNotchedRectangle(),//讓bottomAppBar是一個挖了圓形的矩形
? ? ? ? child: Row(
? ? ? ? ? mainAxisAlignment: MainAxisAlignment.spaceAround,//首尾的寬度等于中間寬度的一半
? ? ? ? ? mainAxisSize: MainAxisSize.max,//表示占滿整個寬度,如果是min表示包裹子控件
? ? ? ? ? children: [
? ? ? ? ? ? IconButton(
? ? ? ? ? ? ? icon: Icon(Icons.home,color: Colors.lightBlue,),
? ? ? ? ? ? ? onPressed: (){
? ? ? ? ? ? ? ? setState(() {
? ? ? ? ? ? ? ? ? _index=0;
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? },
? ? ? ? ? ? ),
? ? ? ? ? ? IconButton(
? ? ? ? ? ? ? icon: Icon(Icons.photo,color: Colors.lightBlue,),
? ? ? ? ? ? ? onPressed: (){
? ? ? ? ? ? ? ? setState(() {
? ? ? ? ? ? ? ? ? _index=1;
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? },
? ? ? ? ? ? ),
? ? ? ? ? ],
? ? ? ? ),
? ? ? ),
? ? );
? }
}
EachView:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class EachView extends StatelessWidget{
? final String title;
? const EachView({Key key, this.title}) : super(key: key);
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? // throw UnimplementedError();
? ? return new Scaffold(
? ? ? body: Center(
? ? ? ? child: Text("$title"),
? ? ? ),
? ? );
? }
}
NewPage:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class NewPage extends StatelessWidget{
? @override
? Widget build(BuildContext context) {
? ? // TODO: implement build
? ? // throw UnimplementedError();
? ? return new Scaffold(
? ? ? body: Center(
? ? ? ? child: Text("NewPage"),
? ? ? ),
? ? );
? }
}
效果:
原文鏈接:https://blog.csdn.net/qq_44280408/article/details/110151147
相關推薦
- 2022-12-05 Python異常?ValueError的問題_python
- 2023-03-13 React中如何設置多個className_React
- 2022-08-17 create-react-app常用自定義配置教程示例_React
- 2022-05-12 Nginx反向代理 對響應網頁中的字符串進行替換設置
- 2023-05-15 Pandas處理DataFrame稀疏數據及維度不匹配數據分析詳解_python
- 2022-09-09 Redis中ZSet的具體使用_Redis
- 2023-11-14 MathType 運行時錯誤‘53’:文件未找到:MathPage.WLL
- 2022-12-27 查看Python安裝路徑幾種方法小結_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支