Flutter布局基础——自定义BottomNavigationBar

2021-08-19 14:53:17 浏览数 (1)

Flutter布局基础——自定义BottomNavigationBar

背景

这里我们来尝试实现一个不规则的 BottomNavigationBar,首先了解两个系统组件: floatingActionButtonBottomAppBar

<!--more-->

floatingActionButton

floatingActionButton用于创建一个浮动的按钮,有两种样式:一种是圆形的纯图标的;一种是椭圆的带有图标或文案的。

示例如下:

代码语言:txt复制
void main() => runApp(const MyApp());

/// This is the main application widget.
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: MyStatelessWidget(),
    );
  }
}

/// This is the stateless widget that the main application instantiates
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Floating Action Button'),
      ),
      body: const Center(
        child: Text('Press the button below'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // Add your onPressed code here!
        },
        child: const Icon(Icons.navigation),
        backgroundColor: Colors.green,
      ),
    );
  }
}

显示效果:

<!--<img src="https://inews.gtimg.com/newsapp_ls/0/13897745469/0.png" style="width:200px" />-->

simulator screen shot - iphone 12 pro max - 2021-08-19 at 10.44.05.pngsimulator screen shot - iphone 12 pro max - 2021-08-19 at 10.44.05.png
代码语言:txt复制
void main() => runApp(const MyApp());

/// This is the main application widget.
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: MyStatelessWidget(),
    );
  }
}

/// This is the stateless widget that the main application instantiates
class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Floating Action Button Label'),
      ),
      body: const Center(
        child: Text('Press the button width a label below!'),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          // Add your onPressed code here!
        },
        label: const Text('Approve'),
        icon: const Icon(Icons.thumb_up),
        backgroundColor: Colors.pink,
      ),
    );
  }
}

显示效果:

<!--<img src="https://inews.gtimg.com/newsapp_ls/0/13897745453/0.png" style="width:200px" />-->

simulator screen shot - iphone 12 pro max - 2021-08-19 at 10.43.45.pngsimulator screen shot - iphone 12 pro max - 2021-08-19 at 10.43.45.png

BottomAppBar

要实现的效果如下:

<!--<img src="https://inews.gtimg.com/newsapp_ls/0/13898273707/0.png" style="width:200px" />-->

simulator screen shot - iphone 12 pro max - 2021-08-19 at 13.54.17.pngsimulator screen shot - iphone 12 pro max - 2021-08-19 at 13.54.17.png

下面来分析如何实现:

body 是 ListView,ListView 的内容从上到下是:

  • 两个SwitchListTile
  • 一个Padding
  • 四个RadioListTile

再然后是 floatingActionButton

再然后是 BottomAppBar

如下:

<!--<img src="https://inews.gtimg.com/newsapp_ls/0/13898292007/0.png" style="width:200px" />-->

企业微信20210819-140137@2x.png企业微信20210819-140137@2x.png

代码如下:

代码语言:txt复制
void main() {
  runApp(const BottomAppBarDemo());
}

class BottomAppBarDemo extends StatefulWidget {
  const BottomAppBarDemo({Key? key}) : super(key: key);

  @override
  State createState() => _BottomAppBarDemoState();
}

class _BottomAppBarDemoState extends State<BottomAppBarDemo> {
  bool _showFab = true;
  bool _showNotch = true;
  FloatingActionButtonLocation _fabLocation =
      FloatingActionButtonLocation.endDocked;

  void _onShowNotchChanged(bool value) {
    setState(() {
      _showNotch = value;
    });
  }

  void _onShowFabChanged(bool value) {
    setState(() {
      _showFab = value;
    });
  }

  void _onFabLocationChanged(FloatingActionButtonLocation? value) {
    setState(() {
      _fabLocation = value ?? FloatingActionButtonLocation.endDocked;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          automaticallyImplyLeading: false,
          title: const Text('Bottom App Bar Demo'),
        ),
        body: ListView(
          padding: const EdgeInsets.only(bottom: 88),
          children: <Widget>[
            SwitchListTile(
              value: _showFab,
              onChanged: _onShowFabChanged,
              title: const Text('Floating Action Button'),
            ),
            SwitchListTile(
              value: _showNotch,
              onChanged: _onShowNotchChanged,
              title: const Text('Notch'),
            ),
            const Padding(
              padding: EdgeInsets.all(16),
              child: Text('Floating action button position'),
            ),
            RadioListTile(
              value: FloatingActionButtonLocation.endDocked,
              groupValue: _fabLocation,
              onChanged: _onFabLocationChanged,
              title: const Text('Docked - End'),
            ),
            RadioListTile(
              value: FloatingActionButtonLocation.centerDocked,
              groupValue: _fabLocation,
              onChanged: _onFabLocationChanged,
              title: const Text('Docked - Center'),
            ),
            RadioListTile(
              value: FloatingActionButtonLocation.endFloat,
              groupValue: _fabLocation,
              onChanged: _onFabLocationChanged,
              title: const Text('Floating - End'),
            ),
            RadioListTile(
              value: FloatingActionButtonLocation.centerFloat,
              groupValue: _fabLocation,
              onChanged: _onFabLocationChanged,
              title: const Text('Floating - Center'),
            ),
          ],
        ),
        floatingActionButton: _showFab
            ? FloatingActionButton(
                onPressed: () {},
                child: const Icon(Icons.add),
                tooltip: 'Create',
              )
            : null,
        floatingActionButtonLocation: _fabLocation,
        bottomNavigationBar: _DemoBottomAppBar(
            fabLocation: _fabLocation,
            shape: _showNotch ? const CircularNotchedRectangle() : null),
      ),
    );
  }
}

class _DemoBottomAppBar extends StatelessWidget {
  const _DemoBottomAppBar({
    this.fabLocation = FloatingActionButtonLocation.endDocked,
    this.shape = const CircularNotchedRectangle(),
  });

  final FloatingActionButtonLocation fabLocation;
  final NotchedShape? shape;

  static final List<FloatingActionButtonLocation> centerLocations =
      <FloatingActionButtonLocation>[
    FloatingActionButtonLocation.centerDocked,
    FloatingActionButtonLocation.centerFloat,
  ];

  @override
  Widget build(BuildContext context) {
    return BottomAppBar(
      shape: shape,
      color: Colors.blue,
      child: IconTheme(
        data: IconThemeData(color: Theme.of(context).colorScheme.onPrimary),
        child: Row(
          children: <Widget>[
            IconButton(
              tooltip: 'Open navigation menu',
              icon: const Icon(Icons.menu),
              onPressed: () {},
            ),
            if (centerLocations.contains(fabLocation)) const Spacer(),
            IconButton(
              tooltip: 'Search',
              icon: const Icon(Icons.search),
              onPressed: () {},
            ),
            IconButton(
              tooltip: 'Favorite',
              icon: const Icon(Icons.favorite),
              onPressed: () {},
            ),
          ],
        ),
      ),
    );
  }
}

要注意floatingActionButtonLocation的设置

参考

  • FloatingActionButton Dev Doc
  • BottomAppBar
  • 20个Flutter实例视频教程 让你轻松上手工作

0 人点赞