2026.04.26 dart  
 2026.04.26 dart  
 2026.04.25 dart  
 2026.04.19 dart  
 2026.04.19 dart  
 2026.04.19 dart  
 2026.04.15 未分類  

Rivercount


main.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CounterNotifier extends Notifier<int> {
  @override
  int build() => 0;
  void increment() => state++;
  void decrement() => state--;
  void reset() => state = 0;
// void increment() {
// state = state + 1;
 // }
}

final counterProvider = NotifierProvider<CounterNotifier, int>(() {
  return CounterNotifier();
});

void main() {
runApp(
 const ProviderScope(
 child: MyApp(),
 ),
);
}

class MyApp extends StatelessWidget {
 const MyApp({super.key});
 @override
  Widget build(BuildContext context) {
   return const MaterialApp(
   home: MyHomePage(title: 'Flutter Count Home Page'),
  );
 }
}

class MyHomePage extends ConsumerWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
@override
Widget build(BuildContext context, WidgetRef ref) {
 final counter = ref.watch(counterProvider);
 return Scaffold(
 appBar: AppBar(
 backgroundColor: Theme.of(context).colorScheme.inversePrimary,
   title: Text(title),
 ),
body: Center(
child: Column(
 mainAxisAlignment: MainAxisAlignment.center,
 children: <Widget>[
  const Text(
  'You have pushed the button Count',
     ),
  Text(
  '$counter', style: const TextStyle(fontSize: 98),
  ),
  ],
 ),
 ),
 floatingActionButton: Column(
  mainAxisAlignment: MainAxisAlignment.end,
  children: [
  FloatingActionButton(
 onPressed: () => ref.read(counterProvider.notifier).increment(),
    child: const Icon(Icons.add),
  ),
  const SizedBox(height: 5),
  FloatingActionButton(
  onPressed: () => ref.read(counterProvider.notifier).decrement(),
  child: const Icon(Icons.remove),
  ),
 const SizedBox(height: 20),
  FloatingActionButton(
   onPressed: () => ref.read(counterProvider.notifier).reset(),
   child: const Icon(Icons.refresh),
   ),  
  ],
 ),
);
}
}
カテゴリー: dart