A month of Flutter: extract post item widget

Yesterday I created the PostList widget to display multiple posts at once. It contained the code for rendering both the list and the individual items. As the rendering logic for the individual items is going to get more complex, I'm going to extract the item rendering code into a PostItem widget.

There aren't any feature changes in this refactor, but the PostItem lays the groundwork for adding the code to render the image, name, date, etc. That complexity should be contained within this new widget.

class PostItem extends StatelessWidget {
  const PostItem();

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Container(
        height: 300.0,
        child: const Center(
          child: Text('Prim Birb'),
        ),
      ),
    );
  }
}

One of the issues I see in some flutter tutorials is having widgets that are overly complex. Widgets that contain to much functionality are hard to read and test, and should be broken up into smaller widgets.

The code for PostsList is now much more concise:

class PostsList extends StatelessWidget {
  const PostsList();

  static const List<int> _items = <int>[0, 1, 2];

  @override
  Widget build(BuildContext context) {
    return ListView(children: _itemList());
  }

  List<PostItem> _itemList() {
    return _items.map((int index) => const PostItem()).toList();
  }
}

Separating the code into multiple widgets makes unit testing easier. Tests for how a PostItem renders content can be contained within post_item_test.dart and posts_list_test.dart only has to test that PostItems are rendered.

Tests for PostItem:

testWidgets('Renders a post', (WidgetTester tester) async {
  // Build our app and trigger a frame.
  await tester.pumpWidget(const MaterialApp(
    home: PostItem(),
  ));

  expect(find.byType(Card), findsOneWidget);
  expect(find.text('Prim Birb'), findsOneWidget);
});

Tests for PostsList:

testWidgets('Renders list of posts', (WidgetTester tester) async {
  // Build our app and trigger a frame.
  await tester.pumpWidget(const MaterialApp(
    home: PostsList(),
  ));

  expect(find.byType(PostItem), findsNWidgets(2));
});

Code changes

Posts in this series

  • A month of Flutter
  • A month of Flutter: create the app
  • A month of Flutter: configuring continuous integration
  • A month of Flutter: continuous linting
  • A month of Flutter: upgrading to 1.0
  • A month of Flutter: initial theme
  • A month of Flutter: no content widget
  • A month of Flutter: a list of posts
  • A month of Flutter: extract post item widget
  • A month of Flutter: post model and mock data
  • A month of Flutter: rendering a ListView with StreamBuilder
  • A month of Flutter: Stream transforms and failing tests
  • A month of Flutter: real faker data
  • A month of Flutter: rendering network images
  • A month of Flutter: FABulous authentication
  • A month of Flutter: configure Firebase Auth for Sign in with Google on Android
  • A month of Flutter: configure Firebase Auth for Sign in with Google on iOS
  • A month of Flutter: Sign in with Google
  • A month of Flutter: mocking Firebase Auth in tests
  • A month of Flutter: delicious welcome snackbar
  • A month of Flutter: navigate to user registration
  • A month of Flutter: user registration form
  • A month of Flutter: testing forms
  • A month of Flutter: setting up Firebase Firestore
  • A month of Flutter: awesome adaptive icons
  • A month of Flutter: set up Firestore rules tests
  • A month of Flutter: Firestore create user rules and tests
  • A month of Flutter: WIP save users to Firestore
  • A month of Flutter: user registration refactor with reactive scoped model
  • A month of Flutter: the real hero animation
  • A month of Flutter: a look back

  • Category: Development
    Tags: Flutter, Tutorial