Search
 
SCRIPT & CODE EXAMPLE
 

DART

flutter: provider ChangeNotifierProvider()

// import Provider package...
// 1. Model
class CounterNotifier with ChangeNotifier {	// Use:---   with ChangeNotifier
  int number = 0;

  void increase() {
    number++;
    notifyListeners();	// --- notifyListeners()
  }

  void decrease() {
    number--;
    notifyListeners();	// --- notifyListeners()
  }
}

// 2. MyApp()
return ChangeNotifierProvider(
  create: (context) => CounterNotifier(),
  child: const MaterialApp(
    home: ChangeNotifierProviderScreen(),
  ),
);

// 3. ChildScreen()
return Scaffold(
  appBar: AppBar(
    title: const Text('Next Page'),
  ),
  body: Center(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(
          onPressed: context.read<CounterNotifier>().increase,	// Use: context.read<T>().property or method
          child: Text(
            '+++ ${context.watch<CounterNotifier>().number}',	// Use: context.watch<T>().property
            style: Theme.of(context).textTheme.headline5,
          ),
        ),
        ElevatedButton(
          onPressed: context.read<CounterNotifier>().decrease,	// Use: context.read<T>().property or method
          child: Text(
            '--- ${context.watch<CounterNotifier>().number}',	// Use: context.watch<T>().property
            style: Theme.of(context).textTheme.headline5,
          ),
        ),
        Consumer<CounterNotifier>(								// Use: Consumer<T>() Widget
          builder: (context, value, child) => Text('${value.number}'),
        ),
        Consumer<CounterNotifier>(								// Use: Consumer<T>() Widget
          builder: (context, value, child) => ElevatedButton(
            onPressed: value.increase,
            child: const Text('Increase'),
          ),
        ),
        Consumer<CounterNotifier>(								// Use: Consumer<T>() Widget							/
          builder: (context, value, child) => ElevatedButton(
            onPressed: value.decrease,
            child: const Text('Decrease'),
          ),
        ),
      ],
    ),
  ),
);
Comment

PREVIOUS NEXT
Code Example
Dart :: flutter logo text color 
Dart :: flutter logo style 
Dart :: openining keyboard overflows pixels in flutter 
Dart :: dart get return value of future function 
Dart :: dart exit function 
Dart :: dart class fields final 
Dart :: what is pubspec.yaml 
Dart :: how to store special characters in dart string 
Dart :: learn flutter and dart to create android and ios apps 
Dart :: dart set.generate 
Dart :: flutter showmodal initstate 
Dart :: flutter map get value by key 
Dart :: flutter provider difference between Consumer<T and context.watch<T 
Dart :: unexpected text late flutter 
Dart :: create array in flutter 
Dart :: constructor with different name flutter 
Dart :: cricle in flutter 
Dart :: glowing buttons in flutter 
Dart :: dart length 
Swift :: center a text in swiftui 
Swift :: Check if device is iPhone or not swift ios 
Swift :: presentviewcontroller must be set swift google login 
Swift :: How to control the line spacing in UILabel 
Swift :: swift bring something to front of view 
Swift :: Save class in userdefaults ios swift 
Swift :: for each swiftui 
Swift :: swift collectionview scrolltoitem 
Swift :: swiftui background color 
Swift :: swift add programmatically constraint to view 
Swift :: standard bank swift code 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =