pure_widget 1.0.0 copy "pure_widget: ^1.0.0" to clipboard
pure_widget: ^1.0.0 copied to clipboard

Base widget which triggers rebuild only if props changed

pure_widget #

lesnitsky.dev GitHub stars Twitter Follow

Base widget which triggers rebuild only if props changed

Installation #

pubspec.yaml:

dependencies:
  pure_widget: ^1.0.0

Example #

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:pure_widget/pure_widget.dart';

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

class TileProps {
  final int value;

  TileProps(this.value);
}

class Tile extends PureWidget<TileProps> {
  final TileProps props;

  const Tile({Key? key, required this.props}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print('rebuild with new value ${props.value}');

    return Card(
      child: Center(
        child: Text(
          props.value.toString(),
          style: Theme.of(context).textTheme.headline1,
        ),
      ),
    );
  }
}

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

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

class _AppState extends State<App> {
  final List<TileProps> values = [TileProps(0), TileProps(0), TileProps(0)];
  final r = Random();

  update() {
    setState(() {
      final index = r.nextInt(3);
      final oldValue = values[index].value;

      values[index] = TileProps(oldValue + 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            ...[
              Tile(props: values[0]),
              Tile(props: values[1]),
              Tile(props: values[2]),
            ].map((e) => Expanded(child: e))
          ],
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: update,
        ),
      ),
    );
  }
}

License #

MIT

6
likes
130
pub points
26%
popularity

Publisher

unverified uploader

Base widget which triggers rebuild only if props changed

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on pure_widget