dart - add a subtitle in flutter code

Dart - add a subtitle in flutter code

Adding a subtitle in Flutter can be done using various widgets depending on where you want the subtitle to appear. Here are some common approaches:

1. Using ListTile

If you want to add a subtitle to a ListTile, you can do so directly using the subtitle property.

Example:

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Subtitle Example')), body: ListView( children: <Widget>[ ListTile( leading: Icon(Icons.info), title: Text('Main Title'), subtitle: Text('This is the subtitle'), ), // Add more ListTiles as needed ], ), ), ); } } 

2. Using Column Widget

If you want more control over the layout, you can use the Column widget to create a custom layout for your title and subtitle.

Example:

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Custom Subtitle Example')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'Main Title', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), SizedBox(height: 8), // Add spacing between title and subtitle Text( 'This is the subtitle', style: TextStyle(fontSize: 16, color: Colors.grey), ), ], ), ), ), ); } } 

3. Using Card Widget

If you want to add a subtitle in a card format, you can use the Card widget along with Column to structure the title and subtitle.

Example:

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Card with Subtitle')), body: Center( child: Card( margin: EdgeInsets.all(16), child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( 'Main Title', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), SizedBox(height: 8), // Add spacing between title and subtitle Text( 'This is the subtitle', style: TextStyle(fontSize: 16, color: Colors.grey), ), ], ), ), ), ), ), ); } } 

4. Using AppBar with bottom Property

If you need a subtitle in the AppBar, you can use the bottom property with a PreferredSize widget to include a subtitle.

Example:

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('AppBar with Subtitle'), bottom: PreferredSize( preferredSize: Size.fromHeight(40.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( 'Main Title', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), ), Text( 'This is the subtitle', style: TextStyle(fontSize: 14, color: Colors.white70), ), ], ), ), ), body: Center(child: Text('Body Content')), ), ); } } 

Summary

  • ListTile: Easy way to add subtitle with built-in properties.
  • Column: Offers flexibility for custom layouts.
  • Card: For a card-like appearance with title and subtitle.
  • AppBar: Use the bottom property to add a subtitle in the app bar.

Choose the method that best fits your UI design and requirements.

Examples

  1. "How to add a subtitle to a Flutter ListTile widget?"

    Description: Add a subtitle to a ListTile widget in Flutter to provide additional information beneath the main title.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('ListTile with Subtitle')), body: ListView( children: <Widget>[ ListTile( title: Text('Main Title'), subtitle: Text('Subtitle Text'), ), ], ), ), ); } } 

    Explanation: Uses ListTile to display a title with a subtitle in a simple list view.

  2. "How to add a subtitle to a Card widget in Flutter?"

    Description: Add a subtitle to a Card widget to enhance the card's content with additional details.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Card with Subtitle')), body: ListView( children: <Widget>[ Card( child: ListTile( title: Text('Card Title'), subtitle: Text('Card Subtitle'), ), ), ], ), ), ); } } 

    Explanation: Integrates a ListTile with a subtitle inside a Card widget to present information attractively.

  3. "How to add a subtitle to a Text widget in Flutter?"

    Description: Add a subtitle by stacking two Text widgets to show a title and a subtitle.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Text with Subtitle')), body: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( 'Main Title', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), Text( 'Subtitle Text', style: TextStyle(fontSize: 16, color: Colors.grey), ), ], ), ), ); } } 

    Explanation: Stacks two Text widgets vertically to create a title and subtitle effect.

  4. "Add subtitle to a ListView item in Flutter"

    Description: Create a ListView with items that include both a title and a subtitle.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('ListView with Subtitle')), body: ListView( children: <Widget>[ ListTile( title: Text('Item 1'), subtitle: Text('Subtitle for Item 1'), ), ListTile( title: Text('Item 2'), subtitle: Text('Subtitle for Item 2'), ), ], ), ), ); } } 

    Explanation: Uses ListView to display multiple items with titles and subtitles.

  5. "Add subtitle with custom style in Flutter"

    Description: Customize the style of a subtitle using the TextStyle widget.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Custom Subtitle Style')), body: ListView( children: <Widget>[ ListTile( title: Text('Title'), subtitle: Text( 'Custom Styled Subtitle', style: TextStyle(fontStyle: FontStyle.italic, color: Colors.blue), ), ), ], ), ), ); } } 

    Explanation: Applies a custom style to the subtitle using TextStyle for a unique appearance.

  6. "Flutter: How to use subtitle in a ListView.builder?"

    Description: Implement a ListView.builder with a title and subtitle for each item.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('ListView.builder with Subtitle')), body: ListView.builder( itemCount: 10, itemBuilder: (context, index) { return ListTile( title: Text('Item $index'), subtitle: Text('Subtitle for Item $index'), ); }, ), ), ); } } 

    Explanation: Uses ListView.builder to dynamically create a list of items with titles and subtitles.

  7. "Add subtitle with icon in Flutter ListTile"

    Description: Add a subtitle along with an icon in a ListTile widget.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('ListTile with Icon and Subtitle')), body: ListView( children: <Widget>[ ListTile( leading: Icon(Icons.star), title: Text('Title with Icon'), subtitle: Text('Subtitle Text'), ), ], ), ), ); } } 

    Explanation: Adds an icon to the leading part of a ListTile, along with a title and subtitle.

  8. "Flutter card with subtitle and image"

    Description: Create a Card widget that includes a subtitle and an image.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Card with Image and Subtitle')), body: ListView( children: <Widget>[ Card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Image.network('https://via.placeholder.com/150'), ListTile( title: Text('Card Title'), subtitle: Text('Card Subtitle'), ), ], ), ), ], ), ), ); } } 

    Explanation: Displays an image followed by a title and subtitle inside a Card widget.

  9. "Adding subtitle to a Drawer item in Flutter"

    Description: Add a subtitle to items within a Drawer.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Drawer with Subtitle')), drawer: Drawer( child: ListView( children: <Widget>[ ListTile( title: Text('Drawer Item 1'), subtitle: Text('Subtitle for Item 1'), ), ListTile( title: Text('Drawer Item 2'), subtitle: Text('Subtitle for Item 2'), ), ], ), ), body: Center(child: Text('Content here')), ), ); } } 

    Explanation: Adds a subtitle to each item in the Drawer, providing extra details in the navigation menu.

  10. "How to create a subtitle with background color in Flutter?"

    Description: Create a subtitle with a background color for emphasis.

    Code:

    import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Subtitle with Background Color')), body: ListView( children: <Widget>[ ListTile( title: Text('Title'), subtitle: Container( color: Colors.blue, padding: EdgeInsets.all(8.0), child: Text( 'Subtitle with Background Color', style: TextStyle(color: Colors.white), ), ), ), ], ), ), ); } } 

    Explanation: Uses a Container with a background color to style the subtitle, making it stand out.


More Tags

webpack-3 android-studio drawer copy-local bitbucket reactstrap partial-views isenabled intel-mkl jupyter-console

More Programming Questions

More Cat Calculators

More Various Measurements Units Calculators

More Investment Calculators

More Biology Calculators