Skip to main content

Using Material Components

1. Using Chip Widget

Property Values Description
label Widget Text or widget to display inside the chip
avatar Widget Optional leading widget (e.g., icon or image)
deleteIcon Widget Icon to show when chip is deletable
onDeleted VoidCallback? Callback when chip is deleted
selected bool Whether the chip is selected (default: false)
onSelected void Function(bool)? Callback when chip is selected/deselected

Example: Basic Chip

Chip(
  label: Text('Flutter'),
)

Example: Deletable Chip

Chip(
  label: Text('Material Design'),
  deleteIcon: Icon(Icons.delete),
  onDeleted: () {
    print('Chip deleted');
  },
)

Example: Selectable Chip

Chip(
  label: Text('Selected'),
  selected: true,
  onSelected: (bool selected) {
    print('Selected: $selected');
  },
)

2. Using FilterChip Widget

Property Values Description
filterChip FilterChip Widget to display a filter chip with label and optional icon
label Widget Text or widget to display on the chip
selected bool Whether the chip is selected or not
onSelected void Function(bool) Callback when the chip is tapped to toggle selection
avatar Widget Optional avatar widget to display on the chip
enabled bool Whether the chip is enabled or disabled

Example: Basic FilterChip Usage

FilterChip(
  label: Text('Option 1'),
  selected: false,
  onSelected: (bool selected) {
    setState(() {
      // Handle selection logic here
    });
  },
)

Example: FilterChip with Avatar

FilterChip(
  label: Text('Option 2'),
  avatar: CircleAvatar(
    backgroundImage: AssetImage('assets/images/avatar.png'),
  ),
  selected: true,
  onSelected: (bool selected) {
    setState(() {
      // Handle selection logic here
    });
  },
)
Note: The FilterChip widget is part of the Material Design components and requires a Scaffold or similar parent to render properly.

3. Using ChoiceChip Widget

Property Values Description
label Widget The widget to display as the chip's label.
selected bool Whether the chip is currently selected.
onSelected void Function(bool)? Callback when the chip is tapped to change selection.
chipStyle ChipStyle Customizes the chip's appearance (e.g., color, shape).
padding EdgeInsets? Padding inside the chip.

Example: Basic ChoiceChip

ChoiceChip(
  label: Text('Option 1'),
  selected: false,
  onSelected: (bool selected) {
    setState(() {
      // Handle selection
    });
  },
)

Example: Selected ChoiceChip

ChoiceChip(
  label: Text('Option 2'),
  selected: true,
  onSelected: (bool selected) {
    setState(() {
      // Handle deselection
    });
  },
)
Note: ChoiceChip is ideal for single-select scenarios within a group of options.

4. Using InputChip and ActionChip Widgets

Property Values Description
label Widget The widget to display as the chip's label
selected bool Whether the chip is currently selected
onSelected void Function(bool)? Callback when the chip is tapped to change selection
avatar Widget? Avatar widget to display before the label
enabled bool Whether the chip is enabled or disabled
deleteButton Widget? Optional delete button to show on the right

Example: InputChip Usage

InputChip(
  label: Text('Flutter'),
  selected: true,
  onSelected: (bool selected) {
    print('Selected: $selected');
  },
)
Property Values Description
label Widget The widget to display as the chip's label
onPressed void Function()? Callback when the chip is tapped
avatar Widget? Avatar widget to display before the label
enabled bool Whether the chip is enabled or disabled
closeButton Widget? Optional close button to show on the right

Example: ActionChip Usage

ActionChip(
  label: Text('Share'),
  onPressed: () {
    print('Share button pressed');
  },
)

5. Using CircularProgressIndicator Widget

Property Values Description
value double Current progress value between 0.0 and 1.0. If null, the widget animates.
backgroundColor Color Background color of the progress indicator.
strokeWidth double Width of the progress stroke.
valueColor Animation<Color> Animation for the color of the progress indicator.

Example: Basic CircularProgressIndicator

import 'package:flutter/material.dart';

class MyProgressIndicator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Progress Example")),
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

Example: Animated CircularProgressIndicator

import 'package:flutter/material.dart';

class AnimatedProgressIndicator extends StatefulWidget {
  @override
  _AnimatedProgressIndicatorState createState() => _AnimatedProgressIndicatorState();
}

class _AnimatedProgressIndicatorState extends State<AnimatedProgressIndicator> with TickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      duration: Duration(seconds: 5),
      vsync: this,
    );
    animation = Tween<double>(begin: 0.0, end: 1.0).animate(controller);
    controller.repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Animated Progress")),
      body: Center(
        child: CircularProgressIndicator(
          value: animation.value,
          strokeWidth: 5.0,
        ),
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

6. Using LinearProgressIndicator Widget

Property Values Description
value double Current progress value between 0.0 and 1.0
backgroundColor Color Background color of the progress bar
valueColor Animation<Color> Color of the progress indicator
minWidth double Minimum width of the progress bar
maxWidth double Maximum width of the progress bar

Example: Basic LinearProgressIndicator

LinearProgressIndicator(
  value: 0.5,
)

Example: Customized LinearProgressIndicator

LinearProgressIndicator(
  value: 0.75,
  backgroundColor: Colors.grey[300],
  valueColor: AlwaysStoppedAnimation(Colors.blue),
)
Note: The value property must be between 0.0 and 1.0 inclusive.

7. Using RefreshIndicator Widget

The RefreshIndicator widget in Flutter provides a pull-to-refresh functionality for lists and other scrollable content. It wraps a scrollable widget and allows users to trigger a refresh by pulling down on the content.

Property Description Values
child The scrollable widget to wrap with refresh functionality. Widget
onRefresh A callback function that is called when the user triggers a refresh. Future<void> Function()
color The color of the refresh indicator's progress spinner. Color
backgroundColor The background color of the refresh indicator. Color
dismissible Whether the refresh indicator can be dismissed by dragging. bool

Example: Basic RefreshIndicator Usage

import &#x27;package:flutter/material.dart&#x27;;

class MyListScreen extends StatefulWidget {
  @override
  _MyListScreenState createState() => _MyListScreenState();
}

class _MyListScreenState extends State<MyListScreen> {
  List<String> items = List.generate(20, (i) => &#x27;Item $i&#x27;);

  Future<void> _refreshData() async {
    // Simulate a network call or data refresh
    await Future.delayed(Duration(seconds: 2));
    setState(() {
      items = List.generate(20, (i) => &#x27;Updated Item $i&#x27;);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(&#x27;Refresh Example&#x27;)),
      body: RefreshIndicator(
        onRefresh: _refreshData,
        child: ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            return ListTile(title: Text(items[index]));
          },
        ),
      ),
    );
  }
}
Note: The RefreshIndicator works best with scrollable widgets like ListView, GridView, or SingleChildScrollView. It does not function properly with non-scrollable widgets.

8. Using Stepper Widget

The Stepper widget in Flutter is used to guide users through a sequence of steps, such as filling out a form or completing a multi-step process. It provides a visual representation of the current step and allows navigation between steps.

Property Description Values
steps A list of Step objects that define the steps in the stepper. List of Step objects
currentStep The index of the currently active step. int
onStepTapped Callback function called when a step is tapped. void Function(int)
onStepContinue Callback function called when the user taps the "Continue" button. void Function()
onStepCancel Callback function called when the user taps the "Cancel" button. void Function()
type Determines the layout of the stepper. Can be StepperType.horizontal or StepperType.vertical. StepperType.horizontal, StepperType.vertical

Example: Basic Stepper

Stepper(
  steps: [
    Step(
      title: Text('Step 1'),
      content: Text('Content for Step 1'),
    ),
    Step(
      title: Text('Step 2'),
      content: Text('Content for Step 2'),
    ),
  ],
  currentStep: 0,
)

Example: Stepper with Navigation

int _currentStep = 0;

Stepper(
  currentStep: _currentStep,
  onStepContinue: () {
    if (_currentStep < 2) {
      setState(() {
        _currentStep += 1;
      });
    }
  },
  onStepCancel: () {
    if (_currentStep > 0) {
      setState(() {
        _currentStep -= 1;
      });
    }
  },
  steps: [
    Step(
      title: Text('Personal Info'),
      content: TextField(
        decoration: InputDecoration(labelText: 'Name'),
      ),
    ),
    Step(
      title: Text('Contact Info'),
      content: TextField(
        decoration: InputDecoration(labelText: 'Email'),
      ),
    ),
    Step(
      title: Text('Confirmation'),
      content: Text('Review your information'),
    ),
  ],
)

9. Using DataTable Widget

Property Values Description
header Widget? Optional header widget to display above the table.
columns List<DataColumn> Required list of columns defining the structure and appearance.
rows List<DataRow> Required list of data rows to display in the table.
showCheckbox bool Whether to show checkboxes for each row (default: false).
sortAscending bool? Controls the sort indicator direction (null for no indicator).
sortColumnIndex int? Index of the column currently being sorted.
onSort void Function(int, bool)? Callback when a column header is tapped to sort.

Example: Basic DataTable Usage

DataTable(
  columns: [
    DataColumn(label: Text('Name')),
    DataColumn(label: Text('Age')),
    DataColumn(label: Text('City')),
  ],
  rows: [
    DataRow(cells: [
      DataCell(Text('John')),
      DataCell(Text('25')),
      DataCell(Text('New York')),
    ]),
    DataRow(cells: [
      DataCell(Text('Jane')),
      DataCell(Text('30')),
      DataCell(Text('Los Angeles')),
    ]),
  ],
)

Example: Sortable DataTable

bool sortAscending = true;
int? sortColumnIndex;

DataTable(
  sortColumnIndex: sortColumnIndex,
  sortAscending: sortAscending,
  onSort: (int columnIndex, bool ascending) {
    setState(() {
      sortColumnIndex = columnIndex;
      sortAscending = ascending;
    });
  },
  columns: [
    DataColumn(
      label: Text('Name'),
      onSort: (int columnIndex, bool ascending) {
        // Handle sorting logic here
      },
    ),
    DataColumn(label: Text('Age')),
  ],
  rows: [
    DataRow(cells: [DataCell(Text('John')), DataCell(Text('25'))]),
  ],
)
Note: The DataColumn and DataRow widgets are used to define the structure and content of the table.

10. Using Tooltip Widget

Property Values Description
message String The text to display in the tooltip
child Widget The widget that will show the tooltip on hover
waitDuration Duration Time to wait before showing the tooltip (default: 400ms)
showDuration Duration How long the tooltip stays visible (default: 1500ms)
decoration TooltipDecoration Customize the tooltip's appearance (optional)

Example: Basic Tooltip

Tooltip(
  message: 'Save your changes',
  child: IconButton(
    icon: Icon(Icons.save),
    onPressed: () { /* Save logic */ },
  ),
)

Example: Tooltip with Custom Duration

Tooltip(
  message: 'This tooltip stays longer',
  waitDuration: Duration(milliseconds: 600),
  showDuration: Duration(seconds: 3),
  child: Text('Hover me'),
)
Note: Tooltips are only shown on desktop and web platforms. On mobile, they do not appear.

Was this article helpful?