forked from OpenFlutter/Flutter-Notebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增 Flutter weekly widget ValueListenableBuilder
- Loading branch information
Showing
9 changed files
with
239 additions
and
35 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...roject/example/flutter_widget_of_the_week/lib/widget/week30_value_listenable_builder.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class Week30 extends StatelessWidget { | ||
final ValueNotifier<int> _counter = ValueNotifier(0); | ||
final ColorProvider _colorProvider = ColorProvider(ColorModel()); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.center, | ||
children: <Widget>[ | ||
ValueListenableBuilder( | ||
valueListenable: _counter, | ||
builder: (context, int value, _) { | ||
return Text('current counter is : $value'); | ||
}), | ||
ValueListenableBuilder<ColorModel>( | ||
valueListenable: _colorProvider, | ||
builder: (context, model, _) { | ||
return Container( | ||
height: 100, | ||
width: 100, | ||
color: model.color, | ||
); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
floatingActionButton: FloatingActionButton(onPressed: () { | ||
_counter.value++; | ||
_colorProvider.changeColor(); | ||
}), | ||
); | ||
} | ||
} | ||
|
||
class ColorModel { | ||
final List<Color> _colors = [ | ||
Colors.redAccent, | ||
Colors.lightGreen, | ||
Colors.pinkAccent, | ||
Colors.tealAccent, | ||
Colors.blue, | ||
]; | ||
int _currentColor = 0; | ||
|
||
get color => _colors[_currentColor]; | ||
|
||
changeColor() { | ||
if (_currentColor != _colors.length - 1) | ||
_currentColor++; | ||
else | ||
_currentColor = 0; | ||
} | ||
} | ||
|
||
class ColorProvider extends ValueNotifier<ColorModel> { | ||
ColorProvider(ColorModel value) : super(value); | ||
|
||
changeColor() { | ||
value.changeColor(); | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
mecury_project/example/provider_example/lib/generated/i18n.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
// ignore_for_file: non_constant_identifier_names | ||
// ignore_for_file: camel_case_types | ||
// ignore_for_file: prefer_single_quotes | ||
|
||
// This file is automatically generated. DO NOT EDIT, all your changes would be lost. | ||
class S implements WidgetsLocalizations { | ||
const S(); | ||
|
||
static const GeneratedLocalizationsDelegate delegate = | ||
GeneratedLocalizationsDelegate(); | ||
|
||
static S of(BuildContext context) => Localizations.of<S>(context, S); | ||
|
||
@override | ||
TextDirection get textDirection => TextDirection.ltr; | ||
|
||
} | ||
|
||
class $en extends S { | ||
const $en(); | ||
} | ||
|
||
class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> { | ||
const GeneratedLocalizationsDelegate(); | ||
|
||
List<Locale> get supportedLocales { | ||
return const <Locale>[ | ||
Locale("en", ""), | ||
]; | ||
} | ||
|
||
LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) { | ||
return (List<Locale> locales, Iterable<Locale> supported) { | ||
if (locales == null || locales.isEmpty) { | ||
return fallback ?? supported.first; | ||
} else { | ||
return _resolve(locales.first, fallback, supported, withCountry); | ||
} | ||
}; | ||
} | ||
|
||
LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) { | ||
return (Locale locale, Iterable<Locale> supported) { | ||
return _resolve(locale, fallback, supported, withCountry); | ||
}; | ||
} | ||
|
||
@override | ||
Future<S> load(Locale locale) { | ||
final String lang = getLang(locale); | ||
if (lang != null) { | ||
switch (lang) { | ||
case "en": | ||
return SynchronousFuture<S>(const $en()); | ||
default: | ||
// NO-OP. | ||
} | ||
} | ||
return SynchronousFuture<S>(const S()); | ||
} | ||
|
||
@override | ||
bool isSupported(Locale locale) => _isSupported(locale, true); | ||
|
||
@override | ||
bool shouldReload(GeneratedLocalizationsDelegate old) => false; | ||
|
||
/// | ||
/// Internal method to resolve a locale from a list of locales. | ||
/// | ||
Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported, bool withCountry) { | ||
if (locale == null || !_isSupported(locale, withCountry)) { | ||
return fallback ?? supported.first; | ||
} | ||
|
||
final Locale languageLocale = Locale(locale.languageCode, ""); | ||
if (supported.contains(locale)) { | ||
return locale; | ||
} else if (supported.contains(languageLocale)) { | ||
return languageLocale; | ||
} else { | ||
final Locale fallbackLocale = fallback ?? supported.first; | ||
return fallbackLocale; | ||
} | ||
} | ||
|
||
/// | ||
/// Returns true if the specified locale is supported, false otherwise. | ||
/// | ||
bool _isSupported(Locale locale, bool withCountry) { | ||
if (locale != null) { | ||
for (Locale supportedLocale in supportedLocales) { | ||
// Language must always match both locales. | ||
if (supportedLocale.languageCode != locale.languageCode) { | ||
continue; | ||
} | ||
|
||
// If country code matches, return this locale. | ||
if (supportedLocale.countryCode == locale.countryCode) { | ||
return true; | ||
} | ||
|
||
// If no country requirement is requested, check if this locale has no country. | ||
if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
String getLang(Locale l) => l == null | ||
? null | ||
: l.countryCode != null && l.countryCode.isEmpty | ||
? l.languageCode | ||
: l.toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters