美文网首页
Flutter-Cupertino风格UI-CupertinoT

Flutter-Cupertino风格UI-CupertinoT

作者: noonez | 来源:发表于2019-05-16 16:57 被阅读0次

    实例中有3个Tab标签Home, School,Self;在School页面添加功能跳转到Home.
    需要注意的地方:

    1. CupertinoTabBar中如果需要指定currentIndex参数选中tab标签,那么就必须在onTap中实现更新选中的tab标签,示例中就要有
    onTap: (index){
       selectedIndex = index;
    }
    

    没有的话再次设置selectedIndex不会刷新UI,是一个比较特殊的地方.

    1. CupertinoPageScaffold的child需要加SafeArea,不然child的垂直坐标会是从屏目顶部开始,加了SafeArea后是从NvBar下方开始
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class CupertinoTabWidget extends StatefulWidget{
      @override
      State<StatefulWidget> createState() {
        return _CupertinoTabWidgetState();
      }
    }
    class _CupertinoTabWidgetState extends State<CupertinoTabWidget>{
      List<BottomNavigationBarItem> _bottomBarItems = [
        BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("Home")),
        BottomNavigationBarItem(icon: Icon(Icons.school), title: Text("School")),
        BottomNavigationBarItem(icon: Icon(Icons.people), title: Text("Self"))
      ];
      Widget getPages(int index){
        switch(index){
          case 0:
            return _HomeCupertinoPage();
            break;
          case 1:
            return _SchoolCupertinoPage(toHome: (){
              print("to home callback.");
              this.selectedIndex = 0;
            },);
          case 2:
            return _SelfCupertinoPage();
            break;
          default:
            return null;
            break;
        }
      }
      Map<String, WidgetBuilder> _routes = {};
    
      int _selectedIndex = 2;
      set selectedIndex(int value){
        if(this.mounted && _selectedIndex != value) {
          setState(() {
            print("to index: $value");
            _selectedIndex = value;
          });
        }
      }
    
      @override
      Widget build(BuildContext context) {
        print("current index: $_selectedIndex");
        return CupertinoTabScaffold(
          tabBar: CupertinoTabBar(
            items: _bottomBarItems,
            currentIndex: _selectedIndex,
            onTap: (index){
              //当有指定选中tab时(_selectedIndex),需要这个设置
              //否则再次设置_selectedIndex不会刷新
              selectedIndex = index;
              print("selected index: $index");
            },
          ),
          tabBuilder: (ctx, index){
            return CupertinoTabView(
              builder: (ctx){
                return this.getPages(index);
              },
              routes: _routes,
            );
          },
        );
      }
    }
    
    class _HomeCupertinoPage extends StatelessWidget{
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          //这里需要加SafeArea,不然child的垂直会是从屏目顶部开始
          //加了SafeArea后是从NvBar下方开始
          child: SafeArea(child: Text("Home")),
    //              child: Text("Home"),
          navigationBar: CupertinoNavigationBar(
            middle: Text("Home"),
          ),
        );
      }
    }
    
    class _SchoolCupertinoPage extends StatelessWidget{
      _SchoolCupertinoPage({@required this.toHome}):super();
      VoidCallback toHome;
    
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          child: SafeArea(
              child: Center(
                child: RaisedButton(
                    onPressed: (){
                      this.toHome();
                    },
                  child: Text("To Home"),
                ),
              )
          ),
          navigationBar: CupertinoNavigationBar(
            middle: Text("School"),
            leading: RaisedButton(
              onPressed: (){
                this.toHome();
              },
              child: Icon(Icons.home),
            ),
          ),
        );
      }
    }
    
    class _SelfCupertinoPage extends StatelessWidget{
      double size = 40.0;
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          child: SafeArea(
            child: Center(
              child: Container(
                width: size,
                height: size,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(size / 2),
                  color: Colors.green
                ),
                child: Center(
                  child: Text(
                      "30",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 20
                      ),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    相关文章

      网友评论

          本文标题:Flutter-Cupertino风格UI-CupertinoT

          本文链接:https://www.haomeiwen.com/subject/kdaiaqtx.html