栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android flutter:Material风格组件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android flutter:Material风格组件

本节通过学习老孟《Flutter实战入门》。
使用Material风格组件需要引用 import ‘package:flutter/material.dart’;
一、MaterialApp
MaterialApp作为顶级容器表示当前App是Material风格的,MaterialApp中设置的样式属性都是全局的,常用的属性,如下:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: ExampleWidget(),
    );
  }
}

二、Scaffold
Scaffold是Material组件的布局容器,可用于展示抽屉(Drawer)、通知(Snack Bar)、及底部导航的效果,属性如下:

class _ExampleWidgetState extends State {
  final TextEditingController _controller = new TextEditingController();
  @override
  Widget build(BuildContext context) {
      return Scaffold(
    appBar: AppBar(title: Text('Flutter 实战入门'),),
    body: Container(
      child: Text('body'),
      alignment: Alignment.center,
    ),
    drawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    endDrawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像(end)'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(onPressed: () {},child: Text("+"),),
    floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
    persistentFooterButtons: List.generate(3, (index) {
      return RaisedButton(onPressed: (){},child: Text('persistent'),textColor: Colors.black,);
    }),
    bottomNavigationBar: Row(
      children: [
        Expanded(
          child: RaisedButton(onPressed: (){},child: Text('微信'),),
          flex: 1,
        ),
        Expanded(
            child: RaisedButton(onPressed: (){},child: Text('通信录'),),
          flex: 1,
        ),
        Expanded(
            child: RaisedButton(onPressed: (){},child: Text('发现'),),
          flex: 1,
        ),
        Expanded(
          child: RaisedButton(onPressed: (){},child: Text('我'),),
          flex: 1,
        ),
      ],
    ),
    bottomSheet: RaisedButton(onPressed: (){},child: Text('bottomSheet'),),
  );
  }

Scaffold未 打开drawable的效果图

Scaffold打开抽屉的效果

三、AppBar
AppBar显示在App的顶部,属性如下:

//AppBar改动
class _ExampleWidgetState extends State {
  final TextEditingController _controller = new TextEditingController();
  @override
  Widget build(BuildContext context) {
       return Scaffold(
    //AppBar :左侧返回按钮,title右侧有3个图标
    appBar: AppBar(
      leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){}),
      title: Text('Flutter 实战入门'),
      actions: [
        IconButton(icon: Icon(Icons.add), onPressed: (){}),
        IconButton(icon: Icon(Icons.dashboard), onPressed: (){}),
        IconButton(icon: Icon(Icons.cached),onPressed: (){},),
      ],
    ),
    body: Container(
      child: Text('body'),
      alignment: Alignment.center,
    ),
    drawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    endDrawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像(end)'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(onPressed: () {},child: Text("+"),),
    floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
    persistentFooterButtons: List.generate(3, (index) {
      return RaisedButton(onPressed: (){},child: Text('persistent'),textColor: Colors.black,);
    }),
    bottomNavigationBar: Row(
      children: [
        Expanded(
          child: RaisedButton(onPressed: (){},child: Text('微信'),),
          flex: 1,
        ),
        Expanded(
            child: RaisedButton(onPressed: (){},child: Text('通信录'),),
          flex: 1,
        ),
        Expanded(
            child: RaisedButton(onPressed: (){},child: Text('发现'),),
          flex: 1,
        ),
        Expanded(
          child: RaisedButton(onPressed: (){},child: Text('我'),),
          flex: 1,
        ),
      ],
    ),
    bottomSheet: RaisedButton(onPressed: (){},child: Text('bottomSheet'),),
  );
    throw UnimplementedError();
  }
  }

AppBar效果

四、BottomNavigationBar
BottomNavigationBar底部导航栏,属性如下:

//修改bottomBar,有点问题
class _ExampleWidgetState extends State {
  final TextEditingController _controller = new TextEditingController();
  int _selectIndex = 0;
  @override
  Widget build(BuildContext context) {
     return Scaffold(
    //左侧返回按钮,title右侧有3个图标
    appBar: AppBar(
      leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){}),
      title: Text('Flutter 实战入门'),
      actions: [
        IconButton(icon: Icon(Icons.add), onPressed: (){}),
        IconButton(icon: Icon(Icons.dashboard), onPressed: (){}),
        IconButton(icon: Icon(Icons.cached),onPressed: (){},),
      ],
    ),
    body: Container(
      child: Text('body'),
      alignment: Alignment.center,
    ),
    drawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    endDrawer: Drawer(
      child: ListView(
        children: [
          DrawerHeader(child: Text('头像(end)'),),
          ListTile(title: Text('我的'),),
          ListTile(title: Text('关于'),),
          ListTile(title: Text('主页'),),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(onPressed: () {},child: Text("+"),),
    floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
    persistentFooterButtons: List.generate(3, (index) {
      return RaisedButton(onPressed: (){},child: Text('persistent'),textColor: Colors.black,);
    }),
    //修改
    bottomNavigationBar: BottomNavigationBar(
      items: [
        BottomNavigationBarItem(
          title: Text('微信'),
          icon: Icon(Icons.chat,color: Colors.black,),
          activeIcon: Icon(Icons.chat,color: Colors.green,),
        ),
        BottomNavigationBarItem(
          title: Text('通讯录'),
          icon: Icon(Icons.whatshot,color: Colors.black,),
          activeIcon: Icon(Icons.whatshot,color: Colors.green,),
        ),
        BottomNavigationBarItem(
          title: Text('发现'),
          icon: Icon(Icons.accessible,color: Colors.black,),
          activeIcon: Icon(Icons.accessible,color: Colors.green,),
        ),
        BottomNavigationBarItem(
          title: Text('我'),
          icon: Icon(Icons.access_alarm,color: Colors.black,),
          activeIcon: Icon(Icons.access_alarm,color: Colors.green,),
        ),
      ],
      iconSize: 24,
      currentIndex: _selectIndex,
      onTap: (index) {
        setState(() {
          _selectIndex = index;
        });
      },
      fixedColor: Colors.green,
      type: BottomNavigationBarType.shifting,
    ),
    bottomSheet: RaisedButton(onPressed: (){},child: Text('bottomSheet'),),
  );
    throw UnimplementedError();
  }
  }

有点问题,哪里有问题,请指教。。。。

五、TabBar
Tab是一排水平的标签,可以来回切换。属性如下:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';

String _storageDir = '';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: ExampleWidget(),
    );
  }
}

// Opens an [alertDialog] showing what the user typed.

class ExampleWidget extends StatefulWidget {
  ExampleWidget({Key key}) : super(key: key);

  @override
  State createState() {
//    return new _ExampleWidgetState();
  return new _TabBar();
    throw UnimplementedError();
  }
}

class _TabBar extends State {
  final List _tabValues = [
    '语文',
    '英语',
    '数学',
    '物理',
    '化学',
    '生物',
    '政治',
    '地理',
    '历史',
  ];
  TabController _controller;
  @override
  void initState() {
    super.initState();
    _controller = TabController(length: _tabValues.length, vsync: ScrollableState(),);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TabBar'),
        bottom: TabBar(
          tabs: _tabValues.map((f) {
            return Text(f);
          }).toList(),
          controller: _controller,
          indicatorColor: Colors.red,
          indicatorSize: TabBarIndicatorSize.tab,
          isScrollable: true,
          labelColor: Colors.amber,
          unselectedLabelColor: Colors.black,
          indicatorWeight: 5.0,
          unselectedLabelStyle: TextStyle(height: 2),
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: _tabValues.map((e){
          return Center(
            child: Text(e),
          );
        }).toList(),
      ),
    );
    throw UnimplementedError();
  }
}


六、Drawer
Drawer是抽屉样式的控件,它的子控件钟一般使用ListView,第一个元素一般使用DrawerHeaader,接下来是ListTile.示例见前文 “二、Scaffold”。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号