Home > Blockchain >  Flutter - How to insert the ad in every nth row (every 4th row) in listview.separated
Flutter - How to insert the ad in every nth row (every 4th row) in listview.separated

Time:09-17

I am using Listview.separated and I am using enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  FocusNode focusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    focusNode.addListener(() {
      print('1:  ${focusNode.hasFocus}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return ListView.separated(
      itemCount: 20,
      separatorBuilder: (context, index) {
        if ((index   1) % 4 == 0) {
          return Container(
            height: 100,
            color: Colors.yellow,
            child: Text('it is ads'),
          );
        } else {
          return Container();
        }
      },
      itemBuilder: (context, index) {
        return Container(
          height: 100,
          color: Colors.blue,
          child: Text('item index: $index'),
        );
      },
    );
  }
}

  • Related