tab_switcher 1.0.1 copy "tab_switcher: ^1.0.1" to clipboard
tab_switcher: ^1.0.1 copied to clipboard

Build your complex app around tabs just like Chrome for Android

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:tab_switcher/tab_count_icon.dart';
import 'package:tab_switcher/tab_switcher.dart';
import 'package:tab_switcher_example/counter_tab.dart';

class DemoSettings {
  static bool openTabsInForeground = true;
  static Brightness brightness = Brightness.light;
  static VoidCallback rebuildRootWidget = () {};
}

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => StatefulBuilder(
        builder: (context, setState) {
          DemoSettings.rebuildRootWidget = () => setState(() {});
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(primarySwatch: Colors.blue, brightness: DemoSettings.brightness),
            home: const MyHomePage(),
            debugShowCheckedModeBanner: false,
          );
        },
      );
}

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    controller = TabSwitcherController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabSwitcherWidget(
        controller: controller,
        appBarBuilder: (context, tab) => tab != null
            ? AppBar(
                elevation: 0,
                title: Text(tab.getTitle()),
                actions: [
                  TabCountIcon(controller: controller),
                  DemoSettingsPopupButton(controller: controller),
                ],
              )
            : AppBar(
                elevation: 0,
                backgroundColor: Theme.of(context).scaffoldBackgroundColor,
                foregroundColor: Theme.of(context).textTheme.bodyText1!.color,
                titleSpacing: 8,
                title: NewTabButton(controller: controller),
                actions: [
                  TabCountIcon(controller: controller),
                  DemoSettingsPopupButton(controller: controller),
                ],
              ),
      ),
    );
  }

  late TabSwitcherController controller;
}

class NewTabButton extends StatelessWidget {
  const NewTabButton({
    Key? key,
    required this.controller,
  }) : super(key: key);

  final TabSwitcherController controller;

  @override
  Widget build(BuildContext context) {
    return IntrinsicWidth(
      child: MaterialButton(
        visualDensity: VisualDensity.compact,
        child: Row(
          children: const [
            Icon(Icons.add),
            SizedBox(width: 8),
            Text('New tab'),
          ],
        ),
        onPressed: () => controller.pushTab(CounterTab(), foreground: DemoSettings.openTabsInForeground),
      ),
    );
  }
}

class DemoSettingsPopupButton extends StatelessWidget {
  const DemoSettingsPopupButton({required this.controller, Key? key}) : super(key: key);

  final TabSwitcherController controller;

  @override
  Widget build(BuildContext context) => PopupMenuButton(
        itemBuilder: (BuildContext context) => [
          PopupMenuItem<String>(
            value: 'foreground',
            child: Text('Open tabs in background: ' + (!DemoSettings.openTabsInForeground).toString()),
          ),
          const PopupMenuItem<String>(
            value: 'theme',
            child: Text('Toggle theme'),
          ),
        ],
        onSelected: (v) {
          if (v == "theme") {
            DemoSettings.brightness = DemoSettings.brightness == Brightness.dark ? Brightness.light : Brightness.dark;
            DemoSettings.rebuildRootWidget();
          }
          if (v == "foreground") {
            DemoSettings.openTabsInForeground = !DemoSettings.openTabsInForeground;
          }
        },
      );
}
34
likes
90
points
29
downloads

Publisher

verified publishernuc134r.io

Weekly Downloads

Build your complex app around tabs just like Chrome for Android

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, scroll_shadow_container

More

Packages that depend on tab_switcher